我有一节课:
class One
def initialize; end
end
Run Code Online (Sandbox Code Playgroud)
我需要使用我自己的构造函数创建一个新类,如下所示:
class Two < One
def initialize(some)
puts some
super
end
end
Two.new("thing")
Run Code Online (Sandbox Code Playgroud)
但是当我启动代码时,我收到了一个错误:
thing
test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
Run Code Online (Sandbox Code Playgroud)
gtd*_*gtd 51
super在这种情况下(没有括号)是一种特殊形式.它使用原始参数调用超类方法.
而是试着打电话
super()
Run Code Online (Sandbox Code Playgroud)