Override ruby constant in subclass so inherited methods use new constant instead of the old?
I've done this by simply redefining the constant in the subclass, and then referring to it in methods as self.class::CONST
in instance methods and self::CONST
in class methods. In your example:
class SuperClass
CONST = "Hello, world!"
def self.say_hello
self::CONST
end
end
class SubClass < SuperClass
CONST = "Hello, Bob!"
end
SubClass.say_hello #=> "Hello, Bob!"
Overriding class constants vs properties
self::
Isn't inheritance-aware and always refers to the class it is being executed in. If you are using php5.3+ you might try static::TEST
as static::
is inheritance-aware.
The difference is that static::
uses "late static binding". Find more information here:
http://php.net/manual/en/language.oop5.late-static-bindings.php
Here's a simple test script I wrote:
<?php
class One
{
const TEST = "test1";
function test() { echo static::TEST; }
}
class Two extends One
{
const TEST = "test2";
}
$c = new Two();
$c->test();
output
test2
Overriding Constants in Java
You can't do that.
You can do this, however:
abstract class A {
public abstract int getConst();
}
public class B extends A {
@Override
public int getConst() { return 1; }
}
public class C extends A {
@Override
public int getConst() { return 2; }
}
public static void main(String[] args){
A a = new B();
System.out.println(a.getConst());
}
Overriding constants from parent class python
I believe PEP8 recommends using capital case for constants, but I usually interpret this as "module-level constants".
On the other hand, this is a class variable and PEP8 doesn't specify the naming for class variables.
But if we look at Google Style Guide for Python, here it clearly states that class constants should also be capitalized:
Global/Class Constants CAPS_WITH_UNDER
Best practice to set constant parameters for inheritance class
Move the definition table_color = "red"
into the very beginning of the class GUI
definition and reference it with self.table_color
in the methods of the class. Moving it into the class makes it a class attribute that will be inherited by derived classes.
However you can override its value in a child class by just redefining it in the child class definition (in the same manner) to override the one in the parent class that would otherwise be inherited.
Here's what I mean:
class GUI:
table_color = "red"
def __init__(self):
self.draw_table()
def draw_table(self):
print("table color: {}".format(self.table_color))
class GUI_child(GUI):
table_color = "blue"
def __init__(self):
super(GUI_child, self).__init__()
gui = GUI() # -> table color: red
gui_child = GUI_child() # -> table color: blue
Note that according the PEP 8 - Style Guide for Python Code, constants should be all uppercase. This means table_color
should be changed to TABLE_COLOR
.
Redefine a constant member variable in inherited class
While fiddling with the compiler trying to make sure my example code made sense, I actually came across the fact that the way I was attempting to define the constants was C++11-specific. That led me to look into the ways it was done before, and I found this question, which shed some light on the matter indirectly.
Defining a variable in this way should be done by having the base class take an argument in its constructor, in the form of:
class A
{
public:
A( const int& type ) : x(type) {}
inline int getX() { return x; }
protected:
const int x;
};
class B : public A
{
public:
B() : A(10) {}
};
class C : public A
{
public:
C() : A(50) {}
};
This will work as intended and allow the constant x to be redefined by inherited classes.
Access property/constant in extended classes
Static members are resolved at compile-time, and adding an ExtendedTest.x
does not affect the also-existing BaseTest.x
, which is what the BaseTest#out()
method is linked to.
To accomplish what you're wanting, you need an overridden method:
public class BaseTest {
public String x() {
return "base";
}
public final void out() {
System.out.println(x());
}
}
public class ExtendedTest extends BaseTest {
@Override
public String x() {
return "extended";
}
}
This pattern is commonly used with an abstract method in the base class or interface to require the subclass to define an attribute such as a name or a key.
Related Topics
Rails: How to Show User's "Last Seen At" Time
Sorting a Two-Dimensional Array by Second Value
Best Practice About Empty Belongs_To Association
Namespacing Thor Commands in a Standalone Ruby Executable
How to Structure a Layout Template in Haml
Best Place to Store Model Specific Constants in Rails 3.1
Which Ruby Gems Support the Facebook API
How to Create a Rest API for a Ruby Application
Rails Collection_Select VS. Select
Rails Routing (Root :To => ...)
What Is Double Method in Rspec For
Loop Through Activerecord::Associations::Collectionproxy with Each
How to Check from Ruby Whether a Process with a Certain Pid Is Running