简体   繁体   English

如何在类中调用超类方法

[英]How to call super class method within class

How can I call super class method within class like: 如何在类中调用超类方法,如:

class A  
  def foo  
    puts "lol"  
  end  
end

class B < A  
  foo  
end

You're trying to call an instance method from within the context of a class. 您正在尝试从类的上下文中调用实例方法。 This is not valid. 这是无效的。

What would work: 什么会起作用:

class A  
  def self.foo  
    puts "lol"  
  end  
end

class B < A  
  foo
end

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

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