Ruby 继承 - 超级初始化获取错误数量的参数

发布于 2024-12-08 17:18:25 字数 1945 浏览 0 评论 0原文

我正在使用 Ruby 并学习 OO 技术和继承,我终于遇到了一个困扰我一段时间的错误。

Person类

class Person
    attr_accessor :fname, :lname, :age

    def has_hat?
        @hat
    end

    def has_hat=(x)
        @hat = x
    end

    def initialize(fname, lname, age, hat)
        @fname = fname
        @lname = lname
        @age = age
        @hat = hat
    end

    def to_s
        hat_indicator = @hat ? "does" : "doesn't"
        @fname + " " + @lname + " is " + @age.to_s + " year(s) old and " + hat_indicator + " have a hat\n"  
    end

    def self.find_hatted()
        found = []
        ObjectSpace.each_object(Person) { |p|
            person = p if p.hat?
            if person != nil
                found.push(person)              
            end
        }
        found
    end

end

程序员类(继承自Person)

require 'person.rb'

class Programmer < Person
    attr_accessor :known_langs, :wpm

    def initialize(fname, lname, age, has_hat, wpm)
        super.initialize(fname, lname, age, has_hat)
        @wpm = wpm
        @known_langs = []
    end

    def is_good?
        @is_good
    end

    def is_good=(x)
        @is_good = x
    end

    def addLang(x)
        @known_langs.push(x)
    end


    def to_s
        string = super.to_s
        string += "and is a " + @is_good ? "" : "not" + " a good programmer\n"
        string += "    Known Languages: " + @known_languages.to_s + "\n"
        string += "    WPM: " + @wpm.to_s + "\n\n"
        string
    end

end

然后在我的主脚本中它在这一行失败

...
programmer = Programmer.new('Frank', 'Montero', 46, false, 20)
...

并出现此错误

./programmer.rb:7:in `initialize': wrong number of arguments (5 for 4) (ArgumentError)
        from ./programmer.rb:7:in `initialize'
        from ruby.rb:6:in `new'
        from ruby.rb:6:in `main'
        from ruby.rb:20

I'm playing with Ruby and learning about OO techniques and inheritance and I've finally hit an error that has eluded me for awhile.

Person Class

class Person
    attr_accessor :fname, :lname, :age

    def has_hat?
        @hat
    end

    def has_hat=(x)
        @hat = x
    end

    def initialize(fname, lname, age, hat)
        @fname = fname
        @lname = lname
        @age = age
        @hat = hat
    end

    def to_s
        hat_indicator = @hat ? "does" : "doesn't"
        @fname + " " + @lname + " is " + @age.to_s + " year(s) old and " + hat_indicator + " have a hat\n"  
    end

    def self.find_hatted()
        found = []
        ObjectSpace.each_object(Person) { |p|
            person = p if p.hat?
            if person != nil
                found.push(person)              
            end
        }
        found
    end

end

Programmer Class (inherits from Person)

require 'person.rb'

class Programmer < Person
    attr_accessor :known_langs, :wpm

    def initialize(fname, lname, age, has_hat, wpm)
        super.initialize(fname, lname, age, has_hat)
        @wpm = wpm
        @known_langs = []
    end

    def is_good?
        @is_good
    end

    def is_good=(x)
        @is_good = x
    end

    def addLang(x)
        @known_langs.push(x)
    end


    def to_s
        string = super.to_s
        string += "and is a " + @is_good ? "" : "not" + " a good programmer\n"
        string += "    Known Languages: " + @known_languages.to_s + "\n"
        string += "    WPM: " + @wpm.to_s + "\n\n"
        string
    end

end

Then in my main script It's failing on this line

...
programmer = Programmer.new('Frank', 'Montero', 46, false, 20)
...

With this error

./programmer.rb:7:in `initialize': wrong number of arguments (5 for 4) (ArgumentError)
        from ./programmer.rb:7:in `initialize'
        from ruby.rb:6:in `new'
        from ruby.rb:6:in `main'
        from ruby.rb:20

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2024-12-15 17:18:25

使用所需参数调用 super,而不是调用 super.initialize。

super(fname, lname, age, has_hat)

call super with required params instead calling super.initialize.

super(fname, lname, age, has_hat)
—━☆沉默づ 2024-12-15 17:18:25

程序员初始化应该是 -

def initialize(fname, lname, age, has_hat, wpm)
    super(fname, lname, age, has_hat)
    @wpm = wpm
    @known_langs = []
end

Programmer initialize should be -

def initialize(fname, lname, age, has_hat, wpm)
    super(fname, lname, age, has_hat)
    @wpm = wpm
    @known_langs = []
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文