我有一个类:
class One
def initialize; end
end
我需要像这样用我自己的构造函数创建一个新类:
class Two < One
def initialize(some)
puts some
super
end
end
Two.new("thing")
但是当我启动代码时,出现错误:
thing
test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
最佳答案
super
在这种情况下(没有括号)是一种特殊形式。它使用原始参数调用父类(super class)方法。
尝试调用
super()
关于ruby - 构造函数覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2570428/