gpt4 book ai didi

swift - 一个类中的多个 Equatable 函数

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:48 25 4
gpt4 key购买 nike

我想知道是否可以在一个类中包含多个 Equatable 函数。我目前的问题来自这个例子:

class Ingredient: Equatable {

static func == (lhs: Ingredient, rhs: Ingredient) -> Bool {
return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price
}

// static func == (lhs: Ingredient, rhs: Ingredient) -> Bool {
// return lhs.ingredientId == rhs.ingredientId && lhs.price == rhs.price && lhs.quantity == rhs.quantity
// }
}

如您所见,我想对相等函数进行注释,但这种方式行不通,因为我已经有了一个。我需要这两个功能都能正常工作。有什么最优解吗?

最佳答案

不,你不能。

根据函数的数学定义:

A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.

一般来说,通过定义多个具有相同名称且采用相同类型的参数的函数,您会产生歧义,从而导致不可预测的结果。所以这种行为会产生编译时错误。

关于swift - 一个类中的多个 Equatable 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com