我有以下课程:
class A
@@one = {1 => "hen", 2 => "di", 3 => "tri", 4 => "tetra"}
@@two = {5 => "penta", 6 => "hexa", 7 => "hepta",
8 => "octa", 9 => "nona"}
def initialize(value)
@value = value
end
def to_s
value = @value
if value <=4
@@one[value]
else
@@two[value]
end
end
end
class B < A
@@one = {1 => "meth", 2 => "eth", 3 => "prop", 4 => "but"}
def initialize(value)
@value = value
end
end
和以下驱动程序:
require_relative 'A.rb'
require_relative 'B.rb'
puts A.new(1)
puts B.new(1)
我希望输出为:
hen
meth
但是 A 类变量 @@one 被 B 类变量 @@one 覆盖。所以我的输出是这样的:
meth
meth
我尝试了类实例变量、类常量和类变量。但两种解决方案都不起作用。