如何在Ruby中连续调用两次super实现多层继承方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计154个文字,预计阅读时间需要1分钟。
pythonclass Animal: def move(self): print(I can move)
class Bird(Animal): def move(self): super().move() print(by flying)
class Penguin(Bird): def move(self): super().move() print(by waddling)
我有以下课程class Animal def move "I can move" end end class Bird < Animal def move super + " by flying" end end class Penguin < Bird def move #How can I call Animal move here "I can move"+ ' by swimming' end end
如何在Penguin中调用Animal的移动方法?我不能使用super.super.move.有什么选择?
谢谢
你可以获得Animal的move实例方法,将它绑定到self,然后调用它:class Penguin < Bird def move m = Animal.instance_method(:move).bind(self) m.call end end
本文共计154个文字,预计阅读时间需要1分钟。
pythonclass Animal: def move(self): print(I can move)
class Bird(Animal): def move(self): super().move() print(by flying)
class Penguin(Bird): def move(self): super().move() print(by waddling)
我有以下课程class Animal def move "I can move" end end class Bird < Animal def move super + " by flying" end end class Penguin < Bird def move #How can I call Animal move here "I can move"+ ' by swimming' end end
如何在Penguin中调用Animal的移动方法?我不能使用super.super.move.有什么选择?
谢谢
你可以获得Animal的move实例方法,将它绑定到self,然后调用它:class Penguin < Bird def move m = Animal.instance_method(:move).bind(self) m.call end end

