从抽象类继承静态变量

发布于 2024-11-01 18:09:41 字数 385 浏览 0 评论 0原文

我有六个类,它们都扩展了同一个抽象类。抽象类有一个静态变量,指向一些 JNI 代码,我只想在每次类实例化时加载一次。

据我了解,这会导致该静态变量的一个实例被实例化,但我想要的是每个扩展类都有自己的静态变量实例,该实例对于给定的子类是唯一的。我想在我的抽象类中编写一些代码来修改和/或释放抽象类。是否可以同时做这两件事?

作为一个例子,我可以编写一个带有变量 foo 的抽象类 bar 和打印 foo 内容的 printFoo 方法。然后我按顺序实例化 fooBar1、fooBar2 和 fooBar3,它们各自扩展 bar 类并将 foo 初始化为静态块中的不同值。如果我调用 foobar1.printFoo 我想打印由 fooBar1 构造函数初始化的 foo 的静态值。

这可以在java中完成吗?

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.

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.

Can this be done in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漆黑的白昼 2024-11-08 18:09:41

您可以近似它,但每个子类都需要单独的静态变量,以阻止子类覆盖彼此的值。最简单的方法是通过 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";
   }
}

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.

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";
   }
}
神妖 2024-11-08 18:09:41

我有类似的问题。看来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();

    }
}

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:

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文