Have a parent class's method access the subclass's constants
I think that you don't really want a constant; I think that you want an instance variable on the class:
class Animal
@noise = "whaargarble"
class << self
attr_accessor :noise
end
def make_noise
puts self.class.noise
end
end
class Dog < Animal
@noise = "bark"
end
a = Animal.new
d = Dog.new
a.make_noise #=> "whaargarble"
d.make_noise #=> "bark"
Dog.noise = "WOOF"
d.make_noise #=> "WOOF"
a.make_noise #=> "whaargarble"
However, if you are sure that you want a constant:
class Animal
def make_noise
puts self.class::NOISE
# or self.class.const_get(:NOISE)
end
end
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.
Accessing a subclass attribute from parent's class method
You can access the __class__
attribute via self
- which will resolve to the Son
class & fetch the attribute:
class Parent:
def fun(self):
print(self.__class__.bar)
class Daughter(Parent):
bar = 2.71
class Son(Parent):
bar = 3.14
daughter = Daughter()
son = Son()
son.fun()
daughter.fun()
Prints:
3.14
2.71
Can only access Parent class variables from method
Java is strongly typed language. That means that, if you want to use methods of child class you have to cast down to child class.
In your example:
...
else if (plantList.get(i) instanceof Fungus) { //check if plant is fungus
System.out.print(plantList.get(i).getSmell());
System.out.print(((Fungus)plantList.get(i)).getThorns()); //downcasting
}
...
Your check using constains
wouldn't work. To check type of object you need to use operator instanceof
.
Java: Parent Methods accessing Subclasses' static variables?
An option is to access the subclasses' static data through an abstract (non-static) method:
abstract public class Parent {
protected abstract int getK();
public void print() {
System.out.println(getK());
}
}
public class Child1 extends Parent {
private static final int CHILD1_K = 1;
protected int getK() { return CHILD1_K; }
}
public class Child2 extends Parent {
private static final int CHILD2_K = 2;
protected int getK() { return CHILD2_K; }
}
Ruby: Accessing calling child class constants from parent class?
This seems to work - it forces constant lookup to scope into the current instance's class
module Foo
class Base
def test
self.class::SOME_CONST
end
end
end
class Bar < Foo::Base
SOME_CONST = 'test'
end
Bar.new.test # => 'test'
Related Topics
Initializing Instance Variable as an Array - Ruby
Celluloid Async Inside Ruby Blocks Does Not Work
How to Use Ruby CSV Converters
Which Ruby Rest API Client for Neo4J
Remove Element at Specific Index from Redis List
Validate That a Value Is in a Certain Range, E.G. 1 <= Val <=2
User Selected CSS Stylesheet in Rails
Undefined Method When Accessing Hash Element
Can't Install Rmagick and Imagemagick on Windows 7
What the Purpose of Bind/Unbind Methods in Ruby
Ssl Error on Http Post (Unknown Protocol)
Howto: Model Scope for Todays Records
Gem Install Rails Does Not Install Rails
How to Host a Rails Web Application