简体   繁体   English

从派生类方法调用基类方法

[英]Calling base class method from derived class method

I have a method in my base class and I need to call this method from my derived class. 我的基类中有一个方法,我需要从派生类中调用此方法。 Is it possible using static methods? 是否可以使用静态方法?

class base < A

  def self.method1
  end

end

class derived < base

  def method2
    base.method1
  end

end

Is it possible in this way? 有可能这样吗? Is it correct? 这是正确的吗?

Yes.. 是..

class Base
  def self.method1
    p "hi"
  end
end
class Derived < Base
  def method2
    self.class.method1
  end
end
Derived.new.method2
# >> "hi"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM