ruby是单继承的。
可以重写基类的方法
class Father
def says
puts "I am father."
end
def fatherInfo
puts "father info."
end
end
class Son < Father
def says
puts "I am son."
end
def sonInfo
puts "son info."
end
end
father = Father.new
son = Son.new
father.says
father.fatherInfo
son.says
son.sonInfo
son.fatherInfo
输出
I am father.
father info.
I am son.
son info.
father info.
派生类可以直接调用基类的构造方法
class Father
def initialize name
@name = name
end
def says
puts "I am father."
end
end
class Son < Father
def says
puts "I am son. name: #{@name}"
end
end
son = Son.new "Tom"
son.says
当然也可以在派生类中使用super来继承