Class inheritance
Inheritance
class Animal
attr_accessor :noise
end
class Pig < Animal
def initialize
@noise = 'Oink!'
end
end
Overide and extend
class Sofa
@@can_open = false
attr_accessor :width, :length
def area
width * length
end
end
class SofaBed < Sofa
@@can_open = true
attr_accessor :length_opened, :is_open
def area
is_open ? width * length_opened : width * length
end
end
Accessing the Superclass
class Chef
def make_dinner
puts "Cook food."
end
end
class AmateurChef < Chef
def make_dinner
puts "Read recipe."
super
puts "Clean up mess."
end
end
- super 리턴값을 다른 변수에 할당할 수 있다.
- super 에 매개변수를 넘겨주는 것이 가능하다. (super 가 매개변수를 받는 경우)