【发布时间】:2011-03-30 01:27:50
【问题描述】:
假设我们需要为树定义一个公共类(或为了解决问题我们需要拥有的一些其他对象)。由于我们的类结构可能相当复杂,所以我更喜欢在定义类方法之后再定义它。我们的普通类BaseTree 和我们的特定类之一Tree 是
class BaseTree
class BaseNode; end
class NodeA < BaseNode; end
end
class Container
class Tree < BaseTree; end
end
定义类结构后,我们为所有节点设置#initialize。
class BaseTree::BaseNode
def initialize x
p x
end
end
如果我们测试它,那么一切都很好
Container::Tree::NodeA.new(1)
# => 1
但是,如果之后我们通过以下方式添加一个方法
class Container::Tree::NodeA
def some_method; end
end
那么它会破坏NodeA 和BaseNode 之间的继承!!
Container::Tree::NodeA.new(2)
# ~> -:30:in `initialize': wrong number of arguments(1 for 0) (ArgumentError)
为了解决这个问题,我们必须明确定义它
class Container
class Tree < BaseTree
class NodeA < BaseNode; end # explicit inheritance
end
end
class Container::Tree::NodeA
def some_method; end
end
或通过以下方式
class Container::Tree::NodeA < Container::Tree::BaseNode
def some_method; end
end
class Container::Tree::NodeA < BaseTree::BaseNode
def some_method; end
end
最后一种方式只需要使用一次——第一次添加方法,以后定义可以跳过父类
class Container::Tree::NodeA
def another_method; end
end
之后它工作正常,但我觉得它很麻烦,特别是如果有很多树类型和许多不同的节点。
有没有更优雅的方式来做这样的定义?
【问题讨论】:
-
也许 ping 核心看看这是否是预期的
-
我不明白你为什么使用类作为命名空间,我希望那里有模块。绑定似乎不匹配,但我不明白这里的问题。顺便说一句,您不定义类方法,而是定义实例方法。
标签: ruby class inheritance