当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – 如何在Ruby中调用super.super方法

来源:互联网 收集:自由互联 发布时间:2021-06-23
我有以下课程 class Animal def move "I can move" endendclass Bird Animal def move super + " by flying" endendclass Penguin Bird def move #How can I call Animal move here "I can move"+ ' by swimming' endend 如何在Penguin中调用Anim
我有以下课程

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
网友评论