开发者

Constructor overriding

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜