[導(dǎo)入]Ruby版的父親打孩子
以前在講多態(tài)的時(shí)候總喜歡舉父親打孩子的例子,今天寫了一個(gè)Ruby版,稍后獻(xiàn)上Ruby語(yǔ)法速查
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
posted on 2007-03-21 23:30 本博客不再更新 閱讀(105) 評(píng)論(0) 編輯 收藏