【发布时间】:2009-07-08 01:57:41
【问题描述】:
所以我知道您不应该直接继承 Fixnum、Float 或 Integer,因为它们没有#new 方法。使用 DelegateClass 似乎可行,但这是最好的方法吗?有谁知道这些类没有#new 的原因是什么?
我需要一个行为类似于 Fixnum 的类,但有一些额外的方法,我希望能够在类中通过 self 引用它的值,例如:
class Foo < Fixnum
def initialize value
super value
end
def increment
self + 1
end
end
Foo.new(5).increment + 4 # => 10
【问题讨论】:
-
告诉我们您真正想要做什么(最终目标),我们会尽力告诉您最好的方法。我认为子分类在这里不合适。
标签: ruby inheritance