调用super时参数数量错误

Vol*_*ort 9 ruby

class A
  def initialize
    print "Hello! "
  end
end

class B <  A
  def initialize(name)
    super
    print "My name is #{name}!"
  end
end

test = B.new("Fred")
Run Code Online (Sandbox Code Playgroud)

我明白了

wrong number of arguments (1 for 0)
Run Code Online (Sandbox Code Playgroud)

但为什么?班级B需要一个论点,我正在给予它.类A不需要任何参数,所以我根本没有传递任何东西super.

Cal*_*Jia 18

你需要使用super()才能在没有参数的情况下调用它.超级本身会自动使用提供给自身的参数调用父级(即"名称")