【发布时间】:2014-09-11 00:37:13
【问题描述】:
考虑Ruby中的以下示例:
class ParentClass
private def method
puts "private method"
end
end
class ChildClass < ParentClass
def method
puts "overridden, but should be private too"
end
end
ParentClass.new.method #=> raises exception
ChildClass.new.method #=> produces "overridden, but should be private too"
如果我无法控制ChildClass 的代码,是否可以让它从ParentClass 继承方法可见性?
【问题讨论】:
-
问题:如果对ChildClass没有控制权,为什么需要保证方法是私有的?也许他们毕竟希望它公开?
-
查找程序中的错误。我的意思是肯定有其他方法,但我很好奇它是否可能! ;)
标签: ruby inheritance methods