您可以近似它,但是每个子类需要单独的静态变量,以阻止子类覆盖其他值.最简单的方法是通过getter getFoo进行抽象,以便每个子类从正确的地方获取foo.
这样的东西
abstract class Bar
{
// you don't have to have this in the base class
// - you could leave out the variable and make
// getFoo() abstract.
static private String foo;
String getFoo() {
return foo;
}
public void printFoo() {
System.out.print(getFoo());
}
}
class Foo1 extends Bar
{
static final String foo1;
public String getFoo() {
return foo1; // return our foo1 value
}
public Foo1() {
foo1 = "myfoo1";
}
}
class Foo2 extends Foo1
{
static final String foo2;
public String getFoo() {
return foo2; // return our foo2 value
}
public Foo2() {
foo2 = "myfoo2";
}
}