今天研究了一些Ruby的继承,跟C++等语言有所不同,直接看代码:
require 'watir'
class TestInherit
#Getter
attr_reader:a
def initialize
#this is for all ids initialization
@a = "10000000986"
end #end of def initialize
end #end of class
class ChildTest < TestInherit
attr_reader:test_a
def initialize
#this is for all ids initialization
@test_a = "456"
end #end of def initialize
end #---end ChildTest
test = ChildTest.new
puts test.test_a
puts test.a
输出:
456
nil
当子类和父类中都有无参数的构造函数是,new一个子类对象,只会初始化子类中的实例变量,父类的构造函数并没有被执行。