本博客不再更新

          本博客不再更新,請訪問http://blog.sina.com.cn/fulaoshi

          統計

          最新評論

          [導入]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

          posted on 2007-03-21 23:30 本博客不再更新 閱讀(107) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 芮城县| 新干县| 荣成市| 新乡县| 武城县| 富川| 广宁县| 泊头市| 兴城市| 黔南| 增城市| 凯里市| 台安县| 和平县| 八宿县| 屏东县| 弋阳县| 翁源县| 分宜县| 湛江市| 兴安县| 深水埗区| 蛟河市| 永清县| 四会市| 怀宁县| 阳高县| 隆德县| 梁平县| 寿宁县| 铁岭县| 中宁县| 河北区| 垫江县| 固原市| 香河县| 龙泉市| 汉阴县| 巴林左旗| 开平市| 卫辉市|