黑松山资源网 Design By www.paidiu.com
在子类里,我们可以通过重载父类方法来改变实体的行为.
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 Student1<Human
| def identify
| print "I'm a student.\n"
| end
| end
nil
ruby> Student1.new.identify
I'm a student.
nil
如果我们只是想增强父类的 identify 方法而不是完全地替代它,就可以用 super.
ruby> class Student2<Human
| 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 Dishonest<Human
| 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 Honest<Human
| def train_toll(age)
| super(age) # pass the argument we were given
| end
| end
nil
ruby> Honest.new.train_toll(25)
Normal fare.
nil
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 Student1<Human
| def identify
| print "I'm a student.\n"
| end
| end
nil
ruby> Student1.new.identify
I'm a student.
nil
如果我们只是想增强父类的 identify 方法而不是完全地替代它,就可以用 super.
ruby> class Student2<Human
| 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 Dishonest<Human
| 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 Honest<Human
| def train_toll(age)
| super(age) # pass the argument we were given
| end
| end
nil
ruby> Honest.new.train_toll(25)
Normal fare.
nil
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2025年02月24日
2025年02月24日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]