【发布时间】:2017-02-20 12:10:25
【问题描述】:
说,我有这样的层次结构:
class A
def some_method
'From A'
end
end
class B < A
def some_method
'From B'
end
end
class C < B
def some_method
# what's here to receive 'From A' ?
end
end
c = C.new
c.some_method # get 'From A'
如果我在 C#some_method 中调用 super,我将收到“来自 B”。
我应该如何实现 C#some_method 在c.some_method 中获取'From A'。
这样做的最佳做法是什么?
【问题讨论】:
-
这可以通过拥有一个从
B分支的B2类来解决,而无需重新定义该方法。 -
所以你是说有时
Cs 是Bs 有时不是?我倾向于在这里同意@bjhaid,C < B听起来不是正确的方法(但我可能是错的)。 -
嗯,
Cs 是Bs,但它们也是As。所以这里的作曲听起来不错。 -
我的评论去哪儿了? :o
标签: ruby oop inheritance