【问题标题】:ruby - class instantiation and initialization (initialize is not called?)ruby - 类实例化和初始化(不调用初始化?)
【发布时间】:2011-08-27 17:02:05
【问题描述】:
class Test

  def initialize 
    puts 'initializing test'
  end

end

class TestB < Test

end

something = Class.new(Test)

上面没有调用超类的initialize方法。如果我这样做了

something = TestB.new

它被调用了。

为什么?

【问题讨论】:

    标签: ruby base-class


    【解决方案1】:

    阅读文档,Class.new(Test) 会生成一个派生类对象,其中 Test 作为其超类。

    您需要对该结果调用 new 以获取打印输出。

    TestA = Class.new(Test)
    something_else = TestA.new
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2013-10-18
      • 1970-01-01
      • 2013-04-14
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多