In Java, the abstract class’s static member will be inherited all through its subclasses.
like
abstract class TopThing{
public TopThing next;
public TopThing(TopThing tt){
this.next = tt
}
private static HashTable<String, TopThing> someTable = new HashTable<String,TopThing>();
protected void add(String name) {
someTable.put(name, this);
public static Parent forName(String name) {
return someTable.get(name) ;
}
}
class SomeSpecific extends TopThing {
public final String name;
public SomeSpecific (String name, TopThing tt) {
super(tt);
this.name = name;
this.add(name);
}
This is I am first time writing in Scala,
the only way I know to achieve the above is using companion object, but it seems does not work for this case
- Can companion object store the private static table? (it seems no…)
- If I can declare the table in the companion object, can it be
referenced from companion class? ( it seems no…) - In the
addmethod, how can the subclass’s instance referred and be inserted into the table? (the question is also aboutthisinaddmethod) - What’s a good practice of this in Scala?
Yes, it can.
Yes, you reference it as
TopThing.someTable(or justsomeTableafterimport TopThing._).Same as in Java:
this. So