校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > rudy 重載方法 詳解

rudy 重載方法 詳解

熱門標簽:征服者企業地圖標注 漯河電銷 中牟外呼系統違法嗎 淮安自動外呼系統供應商 柯城手機地圖如何做地圖標注 巫師3地圖標注魔力之所 天津外呼系統怎么收費 外呼線路從哪里出來的 AI電銷機器人 線路
在子類里,我們可以通過重載父類方法來改變實體的行為.

ruby> class Human
    |   def identify
    |     print "I'm a person.\n"
    |   end
    |   def train_toll(age)
    |     if age  12
    |       print "Reduced fare.\n";
    |     else
    |       print "Normal fare.\n";
    |     end
    |   end
    | end
   nil
ruby> Human.new.identify
I'm a person.
   nil
ruby> class Student1Human
    |   def identify
    |     print "I'm a student.\n"
    |   end
    | end
   nil
ruby> Student1.new.identify
I'm a student.
   nil  


如果我們只是想增強父類的 identify 方法而不是完全地替代它,就可以用 super.

ruby> class Student2Human
    |   def identify
    |     super
    |     print "I'm a student too.\n"
    |   end
    | end
   nil
ruby> Student2.new.identify
I'm a human.
I'm a student too.
   nil  


super 也可以讓我們向原有的方法傳遞參數.這里有時會有兩種類型的人...

ruby> class DishonestHuman
    |   def train_toll(age)
    |     super(11) # we want a cheap fare.
    |   end
    | end
   nil
ruby> Dishonest.new.train_toll(25)
Reduced fare. 
   nil

ruby> class HonestHuman
    |   def train_toll(age)
    |     super(age) # pass the argument we were given
    |   end
    | end
   nil
ruby> Honest.new.train_toll(25)
Normal fare. 
   nil   

標簽:大慶 河池 克拉瑪依 內江 棗莊 西雙版納 南昌 甘孜

巨人網絡通訊聲明:本文標題《rudy 重載方法 詳解》,本文關鍵詞  rudy,重載,方法,詳解,rudy,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《rudy 重載方法 詳解》相關的同類信息!
  • 本頁收集關于rudy 重載方法 詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 沙田区| 岑溪市| 景洪市| 门头沟区| 邯郸市| 龙陵县| 抚松县| 凭祥市| 浏阳市| 贵港市| 延吉市| 林甸县| 墨脱县| 贞丰县| 元阳县| 高尔夫| 南丰县| 榆中县| 翼城县| 东台市| 龙门县| 文山县| 澳门| 伊吾县| 榆林市| 光泽县| 巩义市| 马龙县| 迁安市| 阿城市| 江达县| 出国| 喜德县| 都昌县| 苍南县| 新兴县| 屏东市| 广昌县| 孝感市| 平凉市| 阜城县|