我定義的類User
,並推翻其==
操作是這樣的:運算符`!=`是否使用`==`?
class User
attr_reader :age
def initialize age
@age = age
end
def ==(other_user)
return true if @age == other_user.age
false
end
end
的是否使用!=
的==
默認的實現?我是否也需要重寫!=
?
_Sidenote_:'=='是**不是運算符**,它是一個在[在這個特殊情況下]'用戶'類中定義的方法。 – mudasobwa
@mudasobwa其實它既是。 'def =='定義了一個*方法*,當使用'=='*運算符*時被調用。 – Stefan
@斯特凡恩,是的;我的意思是OP中提到的是一種方法,而不是操作員。 – mudasobwa