[導入]Ruby版的父親打孩子
以前在講多態的時候總喜歡舉父親打孩子的例子,今天寫了一個Ruby版,稍后獻上Ruby語法速查
class Father
? attr_accessor :name
?
? def initialize(name)
??? @name =
name
? end
?
? def beat(child)
???
???
child.beaten
???
? end
?
end
class Son < Father
? def beaten
??? puts @name +
" is running away while father beat him";
? end
?
end
class Daughter < Father
? def beaten
??? puts @name +
" is crying while father beat her";
? end
?
end
father = Father.new('Father')
son = Son.new('Son')
daughter = Daughter.new('Daughter')
father.beat(son)
father.beat(daughter)
文章來源:http://blog.sina.com.cn/u/4a5ca02401000761