我有一个包含相等的基类?方法。然后我继承了那个对象,想要使用相等的?超类中的方法作为平等的一部分?子类中的方法。
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类对象,以便进行比较?
干杯
丹
答案 0 :(得分:3)
class A
attr_accessor :a, :b
def equal? other
a == other.a and b == other.b
end
end
class B < A
attr_accessor :c
def equal? other
# super(other) calls same method in superclass, no need to repeat
# the method name you might be used to from other languages.
super(other) && c == other.c
end
end
x = B.new
x.a = 1
y = B.new
y.a = 2
puts x.equal?(y)