从 Ruby 中的子类方法调用父类中的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18448831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:08:44  来源:igfitidea点击:

Calling method in parent class from subclass methods in Ruby

ruby

提问by Passionate Engineer

I've used superto initialize parent class but I cannot see any way of calling parent class from subclass methods.

我曾经super初始化父类,但我看不到任何从子类方法调用父类的方法。

I know PHP and other languages do have this feature but cannot find a good way to do this in Ruby.

我知道 PHP 和其他语言确实具有此功能,但在 Ruby 中找不到实现此功能的好方法。

What would one do in this situation?

在这种情况下,人们会怎么做?

回答by Mark Meyer

If the method is the same name, i.e. you're overriding a method you can simply use super. Otherwise you can use an alias_methodor a binding.

如果方法名称相同,即您要覆盖一个可以简单地使用的方法super。否则,您可以使用 analias_method或 a 绑定。

class Parent
  def method
  end
end

class Child < Parent
  alias_method :parent_method, :method
  def method
    super
  end

  def other_method
    parent_method
    #OR
    Parent.instance_method(:method).bind(self).call
  end
end

回答by maerics

The superkeywordcalls a method of the same name in the super class:

super关键字调用父类的同名方法:

class Foo
  def foo
    "#{self.class}#foo"
  end
end

class Bar < Foo
  def foo
    "Super says: #{super}"
  end
end

Foo.new.foo # => "Foo#foo"
Bar.new.foo # => "Super says: Bar#foo"

回答by Graham Swan

The superkeyword in Ruby actually calls a method of the same name in the parent class. (source)

superRuby 中的关键字实际上调用了父类中的同名方法。(来源

class Foo
  def foo
    # Do something
  end
end

class Bar < Foo
  def foo
    super # Calls foo() method in parent class
  end
end

回答by Christopher Oezbek

Others have said it already well. Just one additional note of caution:

其他人已经说得很好。还有一个额外的注意事项:

The syntax super.footo call method fooin the super class is not supported. Rather it will call the super-method and on the returned result, try to call foo.

不支持在超类中super.foo调用方法的语法。相反,它会调用-method 并在返回的结果上尝试调用.foosuperfoo

  class A
    def a
      "A::a"
    end
  end

  class B < A
    def a
      "B::a is calling #{super.a}" # -> undefined method `a` for StringClass 
    end
  end

回答by Aravind Vemula

class Parent
  def self.parent_method
    "#{self} called parent method"
  end

  def parent_method
    "#{self} called parent method"
  end
end

class Child < Parent

  def parent_method
    # call parent_method as
    Parent.parent_method                # self.parent_method gets invoked
    # call parent_method as
    self.class.superclass.parent_method # self.parent_method gets invoked
    super                               # parent_method gets invoked 
    "#{self} called parent method"      # returns "#<Child:0x00556c435773f8> called parent method"
  end

end

Child.new.parent_method  #This will produce following output
Parent called parent method
Parent called parent method
#<Child:0x00556c435773f8> called parent method
#=> "#<Child:0x00556c435773f8> called parent method"

self.class.superclass == Parent #=> true

self.class.superclass == Parent #=> true

Parent.parent_methodand self.class.superclasswill call self.parent_method(class method) of Parentwhile supercalls the parent_method(instance method) of Parent.

Parent.parent_method并且self.class.superclass会调用while 的 self.parent_method(类方法)调用 的(实例方法)。Parentsuperparent_methodParent