class A #define class level attribute called key class << self attr_accessor :key end end class B < A end B.key = "foo" B.key # returns "foo" A.key # returns nil
.
如果我希望a.key在上面的场景中返回“foo”,该方法是什么?
我知道的唯一方法是手动声明类函数。子类将返回父类的值,但不能让它们返回其他值。
class A def self.key @@key end def self.key=(new_val) @@key = new_val end end
类方法不能是虚拟的。这是我的梦想。当您有一个类时,您没有虚拟表。