【问题标题】:Unable to access method defined in child class from parent class's #inherited method (Ruby)无法从父类的#inherited 方法访问子类中定义的方法(Ruby)
【发布时间】:2015-03-12 23:56:13
【问题描述】:

我正在尝试做的事情:

class GrandFoo
    def self.inherited(subclass)
        puts subclass.new.respond_to? :bar  #false
    end
end

class Foo < GrandFoo
    def bar
        puts 'bar'
    end
end

# but
puts Foo.new.respond_to? :bar  #true

我无法找到有关此案例的信息,实际上我不确定我应该寻找什么。 目标是“要求”在特定目录中找到的文件列表,每个文件都包含具有相同 'bar' 方法的子类。

【问题讨论】:

    标签: ruby oop inheritance


    【解决方案1】:

    The documentation for Class#inherited 说:

    每当创建当前类的子类时调用回调。

    它说已创建,而不是已创建。据推测,这意味着无法保证类的“完整”程度,只有在创建过程中

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      相关资源
      最近更新 更多