java 从抽象类继承静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5667764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inheriting static variable from abstract class
提问by drew
I have half a dozen classes which all extend the same abstract class. The abstract class has a static variable pointing to some JNI code that I only want to load once per instantiation of the classes.
我有六个类,它们都扩展了相同的抽象类。抽象类有一个指向一些 JNI 代码的静态变量,我只想在每次实例化类时加载一次。
From what I understand this results in exactly one instance of this static variable being instantiated, but what I want is for each of the extending classes to have their own static instance of the variable that is unique for the given child class. I want to write some code in my abstract class that modifies and/or releases the abstract class. Is it possible to do both of these things at once?
根据我的理解,这会导致这个静态变量的一个实例被实例化,但我想要的是每个扩展类都有自己的静态变量实例,该实例对于给定的子类是唯一的。我想在我的抽象类中编写一些修改和/或释放抽象类的代码。是否可以同时做这两件事?
So as an example can I write an abstract class bar with an variable foo and a printFoo method which prints the content of foo. Then I instantiate in order fooBar1, fooBar2, and fooBar3 which each extend the bar class and initialize foo to different values in static blocks. If I call foobar1.printFoo
I want to print the static value of foo initialized by fooBar1 constructor.
因此,作为示例,我可以编写一个带有变量 foo 的抽象类 bar 和一个打印 foo 内容的 printFoo 方法。然后我依次实例化 fooBar1、fooBar2 和 fooBar3,它们每个都扩展了 bar 类并将 foo 初始化为静态块中的不同值。如果我打电话,foobar1.printFoo
我想打印由 fooBar1 构造函数初始化的 foo 的静态值。
Can this be done in java?
这可以在java中完成吗?
采纳答案by mdma
You can approximate it, but you will need separate static variables for each subclass, to stop subclasses overwriting each others values. It's easiest to abstract this via a getter getFoo
so that each subclass fetches the foo from the right place.
您可以近似它,但您需要为每个子类使用单独的静态变量,以阻止子类覆盖彼此的值。最简单的方法是通过 getter 将其抽象出来,getFoo
以便每个子类从正确的位置获取 foo。
Something like this
像这样的东西
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";
}
}
回答by Patrick
I have a similar problem. Looks like Java can't isolate static members (attributes). I ended up adding an abstract method instead of the attribute:
我有一个类似的问题。看起来Java不能隔离静态成员(属性)。我最终添加了一个抽象方法而不是属性:
public abstract class Abs {
public void printX() {
System.out.println("For " + this.getClass() + " x=" + getX());
}
protected abstract Integer getX();
}
public class A extends Abs {
protected static Integer x = 1;
@Override
protected Integer getX() {
return x;
}
}
public class B extends Abs {
protected static Integer x = 2;
@Override
protected Integer getX() {
return x;
}
}
public class test {
public static void main(String args[]) {
Abs a = new A();
a.printX();
Abs b = new B();
b.printX();
Abs c = new A();
a.printX();
b.printX();
c.printX();
}
}