given this code:
class A CONST = 'A' def initialize puts CONST end end class B < A CONST = 'B' end A.new # => 'A' B.new # => 'A'
I'd like B
to use the CONST = 'B'
definition, but I don't know how. Any ideas?
Greetings
Tom
It sounds like these "constants" aren't very constant. But in any case, you can't override variables.
An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be virtual , abstract , or override .
you can override properties just like methods. That is, if the base method is virtual or abstract. You should use "new" instead of "override". "override" is used for "virtual" properties.
class A CONST = 'A' def initialize puts self.class::CONST end end class B < A CONST = 'B' end A.new # => 'A' B.new # => 'B'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With