引用整个应用程序中在一个类中创建的静态对象 [英] Referencing static object created in one class throughout entire application

查看:82
本文介绍了引用整个应用程序中在一个类中创建的静态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在基类中创建两个静态对象的java应用程序,这些对象需要在程序的各个类中引用。

I have a java application that creates two static objects in a base class, these objects needs to references throughout classes in the program.

public class Baseclass{
    public static ClassA A = new ClassA();
    public static ClassB B = new Classb();
    ...
    ...
    ...
}   

这些对象在其他类中被引用为本地私有变量。

These objects are referenced in the other classes as a local private variables.

public class ClassA{
    private ClassB b = Baseclass.B;

但是,两个对象都需要彼此运行,如果我创建了其中一个对象的新实例在创建另一个之前,upper类中的局部变量设置为null。 Java中是否有任何概念将对象的实际对象(如指针)作为变量引用而不是制作对象的副本?

However, both object require each other to function and if I creates a new instance of one of the objects before the other is created, the local variable in the "upper" classes is set to null. Is there any concepts in Java that would reference the actual object (like a pointer) to the object as a variable instead of making a copy of the object?

推荐答案

我认为你要找的答案是单身模式。这是您只创建一个类的一个实例以便在其他地方使用的地方。这是一个阅读的好链接。这里是维基百科页面上的一些java示例。

I think the answer you are looking for is a "singleton pattern". This is where you create just one instance of a class for use in other places. Here's a good link to read. Here's the wikipedia page on it with some java examples.

所以你的代码看起来像这样:

So your code would look something like this:

public class A {
    private final static A instance = new A();

    /* private constructor forces you to use the getInstance() method below */
    private A() {}

    public static A getInstance() {
      return instance;
    }
}

然后,无论您想要获得A的实例,会做类似的事情:

Then wherever you want to get an instance of A you would do something like:

public class B {
    private final A classA = ClassA.getInstance();
    ...
}

没有理由 A 也不能有 B 的实例,并调用 B 的方法用自己的方法。你不能用这个交叉依赖关系来调用构造函数中的任何其他方法。

There is no reason why A could not also have an instance of B and call B's methods in its own methods. What you cannot do with this cross dependency is call any of the other's methods in the constructor.

顺便说一下,这些模式应该谨慎使用。实现此目的的更好方法是通过依赖注入而不是全局引用。交叉注射是可能的,但同样应该谨慎使用。一个更好的解决方案是重构类以具有线性依赖性。

In general, by the way, these patterns should be used sparingly. A better way to accomplish this is through dependency injection instead of global references. Cross injection is possible but again, it should be used sparingly. A better solution would be to refactor the classes to have linear dependencies.


Java中是否存在可引用实际对象的任何概念(如一个指针)作为变量而不是制作对象的副本?

Is there any concepts in Java that would reference the actual object (like a pointer) to the object as a variable instead of making a copy of the object?

Java是按值传递但值是any对象是对象的引用(类似于C中的指针,尽管它们是一个内存地址)。因此,如果您有一个 A 的实例并将其分配给另一个字段,那么该字段将是相同的值,并将引用相同的 A实例

Java is pass by value but the value of any Object is the reference to the object (similar to pointers in C although they are not a memory address). So if you have an instance of A and assign it to another field, that field will be the same value and will be referencing the same instance of A.

// instantiate a new instance of A
A a1 = new A();
// assign the reference to A to another variable
a2 = a1;
// they are equivalent and both reference the same object
if (a1 == a2) ...

这篇关于引用整个应用程序中在一个类中创建的静态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆