【发布时间】:2009-12-13 10:39:10
【问题描述】:
我有一个包含相等的基类?方法。然后我继承了该对象并想使用等于?超类中的方法作为equal的一部分?子类中的方法。
class A
@a
@b
def equal?(in)
if(@a == in.a && @b == in.b)
true
else
false
end
end
end
class B < A
@c
def equal?(in)
#This is the part im not sure of
if(self.equal?in && @c == in.c)
true
else
false
end
end
end
如何在子类中引用继承的 A 类对象进行比较?
干杯
丹
【问题讨论】:
-
我在这里为这个特定问题提供了非常详细的答案:StackOverflow.Com/questions/1830420/…。它也适用于您的问题。
-
在ruby中比较两个对象的方法通常命名为
==而不是equal?
标签: ruby inheritance oop