给定这段代码:
class A
CONST = 'A'
def initialize
puts CONST
end
end
class B < A
CONST = 'B'
end
A.new # => 'A'
B.new # => 'A'
我希望 B
使用 CONST = 'B'
定义,但我不知道如何使用。有什么想法吗?
问候
汤姆
最佳答案
class A
CONST = 'A'
def initialize
puts self.class::CONST
end
end
class B < A
CONST = 'B'
end
A.new # => 'A'
B.new # => 'B'
关于ruby - 如何在继承类中使用重写常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3174563/