简体   繁体   English

我如何在 Java 的不同类中使用相同的 object

[英]How can i use same object in different classes in Java

Suppose i have 3 java classes A, B and C假设我有 3 个 java 类 A、B 和 C

I need to create an object of class C that is used in both A and B but the problem in creating the object separately is that the constructor of class c is called 2 times. I need to create an object of class C that is used in both A and B but the problem in creating the object separately is that the constructor of class c is called 2 times. But i want the constructor to be called just once.但我希望构造函数只被调用一次。

So i want to use the object created in class A into class b.所以我想使用在 class A 中创建的 object 到 class b 中。

So create the object once, and pass it into the constructors of A and B:因此创建一次 object,并将其传递给 A 和 B 的构造函数:

C c = new C();
A a = new A(c);
B b = new B(c);

...

public class A 
{
    private final C c;

    public A(C c)
    {
        this.c = c;
    }
}

Note that this is very common in Java as a form of dependency injection such that objects are told about their collaborators rather than constructing them themselves.请注意,这在 Java 中非常常见,作为一种依赖注入形式,对象被告知其合作者,而不是自己构建它们。

Create object C outside of both A and B and have the constructors of A and B accept an instance of class C.在 A 和 B 之外创建 object C 并让 A 和 B 的构造函数接受 class Z0D61F8370CAD1D4E122 的实例

class A {
   public A( C c ) {
       ....
   }
}

class B {
   public B( C c ) {
       ....
   }
}

Alternatively, if the constructors for A and B are not called near each other you could define Class c as a singleton.或者,如果 A 和 B 的构造函数没有在彼此附近调用,您可以将 Class c 定义为 singleton。 See http://mindprod.com/jgloss/singleton.htmlhttp://mindprod.com/jgloss/singleton.html

you would then do:然后你会做:

A a = new A(C.getInstance());
B b = new B(C.getInstance());

or alternatively, in constructors for A and B just call getInstance instead of the constructor, ie.或者,在AB的构造函数中,只需调用 getInstance 而不是构造函数,即。

// C c = new C();
C c = C.getInstance();

You want to share an instance?你想共享一个实例? Ok, how about his:好吧,他的呢:

C c = new C();
B b = new B(c);
A a = new A(c);

Another option is:另一种选择是:

B b = new B();
A a = new A(b.c);

暂无
暂无

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

相关问题 如何从不同的类访问同一个对象 - How can I have access to the same object from different classes 如何在不同的类型ArrayList中使用相同的方法? - How can I use same methods in different classes type ArrayList? Java-如何在多个类中使用同一对象 - Java - how to use the same object in many classes 我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇) - How can i use the return value of a private static method (extension of object) in a different method in that same class? (java) 我想使用来自不同类的数据库,我该如何在 JAVA 中做到这一点? (AndroidStudio/Java) - I want so use a Database from different classes, how can I do that in JAVA? (AndroidStudio/Java) 如何使用相同的变量名实例化不同类的对象 - How to use same variable name to instantiate the object of different classes Java模板,如何使用两个具有相同名称和不同类型的类 - Java Templates, how to use two classes with the same name and different types 我如何同时表示应该是2个不同类的对象 - How can I express object which should be 2 different classes at the same time 我可以将Java序列化对象反序列化为不同的类吗? - Can I DeSerialize a java serialized object to different classes? 对于 Ignite,如何在不覆盖 Marshaller 的情况下在不同的包中使用相同的类? - For Ignite, how can I use the same classes in different packages without overriding Marshaller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM