0
我有一個基類,它包含一個等於?方法。我已經繼承了那個對象並且想要使用平等的?方法在超類中作爲平等的一部分?方法在子類中。比較兩個繼承對象Ruby
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類對象,以便我可以進行比較?
乾杯
丹
這有點http://StackOverflow.Com/questions/1830420/ – 2009-12-13 11:56:26
我提供了一個非常詳細的回答這個特定問題在這裏重複的:HTTP: //StackOverflow.Com/questions/1830420/is-it-possible-to-compare-private-attributes-in-ruby/1832634/#1832634。它也適用於你的問題。 – 2009-12-13 11:58:54
在ruby中,比較兩個對象的方法通常命名爲'=='而不是'equal'' – johannes 2009-12-13 13:01:44