如何使用Ruby on Rails实现调用嵌套的super方法?

更新于
2026-10-03 21:15:06
2阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计151个文字,预计阅读时间需要1分钟。

如何使用Ruby on Rails实现调用嵌套的super方法?

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()

我有以下课程

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,然后调用它:

如何使用Ruby on Rails实现调用嵌套的super方法?

class Penguin < Bird def move m = Animal.instance_method(:move).bind(self) m.call end end

本文共计151个文字,预计阅读时间需要1分钟。

如何使用Ruby on Rails实现调用嵌套的super方法?

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()

我有以下课程

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,然后调用它:

如何使用Ruby on Rails实现调用嵌套的super方法?

class Penguin < Bird def move m = Animal.instance_method(:move).bind(self) m.call end end