简体   繁体   English

从抽象类继承静态变量

[英]Inheriting static variable from abstract class

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. 因此,作为一个例子,我可以编写一个带有变量foo的抽象类栏和一个打印foo内容的printFoo方法。 Then I instantiate in order fooBar1, fooBar2, and fooBar3 which each extend the bar class and initialize foo to different values in static blocks. 然后我按顺序实例化fooBar1,fooBar2和fooBar3,每个扩展bar类并将foo初始化为静态块中的不同值。 If I call foobar1.printFoo I want to print the static value of foo initialized by fooBar1 constructor. 如果我调用foobar1.printFoo我想打印由fooBar1构造函数初始化的foo的静态值。

Can this be done in java? 这可以在java中完成吗?

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 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";
   }
}

I have a similar problem. 我有一个类似的问题。 Looks like Java can't isolate static members (attributes). 看起来Java无法隔离静态成员(属性)。 I ended up adding an abstract method instead of the attribute: 我最后添加了一个抽象方法而不是属性:

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();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM