子类
继承允许类根据现有类定义特定行为。
class Animal
def say_hello
'Meep!'
end
def eat
'Yumm!'
end
end
class Dog < Animal
def say_hello
'Woof!'
end
end
spot = Dog.new
spot.say_hello # 'Woof!'
spot.eat # 'Yumm!'
在这个例子中:
Dog继承自Animal,使其成为一个子类。Dog从Animal获得了say_hello和eat方法。Dog覆盖了具有不同功能的say_hello方法。