小编Tom*_*Tom的帖子

如何在继承类中使用重写常量

鉴于此代码:

class A
  CONST = 'A'

  def initialize
    puts CONST
  end
end

class B < A
  CONST = 'B'
end

A.new # => 'A'
B.new # => 'A'
Run Code Online (Sandbox Code Playgroud)

我想B使用这个CONST = 'B'定义,但我不知道如何.有任何想法吗?

问候

汤姆

ruby inheritance class constants

33
推荐指数
1
解决办法
8654
查看次数

标签 统计

class ×1

constants ×1

inheritance ×1

ruby ×1