开发者

Constructor overriding

开发者 https://www.devze.com 2022-12-25 06:49 出处:网络
I have a class: class One def initialize; end end I need to create a new class with my own constructor like this:

I have a class:

class One
  def initialize; end
end

I need to create a new class with my own constructor like this:

class Two < One
  def initialize(some)
    puts some
    super
  end
end

Two.new("thing")

but when I laun开发者_开发问答ch the code, I got an error:

thing
test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)


super in this case (without parentheses) is a special form. It calls the superclass method with the original params.

Instead try calling

super()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号