Java:在各种其他类中使用类的相同实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7743210/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:14:18  来源:igfitidea点击:

Java: Using same instance of a class in various other classes

javaooparchitecturestaticconstructor

提问by nano7

it might be stupid question, but I dont know the answer to it and I dont know where to search the answer, so it would be nice if someone could help me.

这可能是一个愚蠢的问题,但我不知道答案,也不知道在哪里搜索答案,所以如果有人能帮助我就好了。

I've a class (lets name it A) with different members and methods. I use the methods of this class in another class (lets name it B).

我有一个具有不同成员和方法的类(让我们将其命名为 A)。我在另一个类中使用了这个类的方法(让我们将其命名为 B)。

For every B-Object created I want to use the SAME instance of A. Is that possible? Actually I have a constructor in B where I call A a = new A(); Of course I always get different instances of this class.

对于创建的每个 B 对象,我都想使用 A 的相同实例。这可能吗?实际上我在 B 中有一个构造函数,我在其中调用 A a = new A(); 当然,我总是得到这个类的不同实例。

How can I now change this? I know it could be possible to solve it with spring framework (inject always the same object into the instances of B), but I cant use it. How else could this problem be solved?

我现在怎样才能改变这种情况?我知道可以用 spring 框架解决它(总是将相同的对象注入 B 的实例),但我不能使用它。这个问题还能怎么解决?

Thank you very much for your help! :-)

非常感谢您的帮助!:-)

采纳答案by hvgotcodes

Yes its possible. You need to define a singleton instance of classA that is static, and use it wherever you want.

是的,它可能。您需要定义一个静态的 classA 的单例实例,并在您想要的任何地方使用它。

So there are multiple ways of doing this:

所以有多种方法可以做到这一点:

public class ClassA {
   public static ClassA classAInstance = new ClassA();  

}

then anywhere you can do

然后你可以做的任何地方

ClassA.classAInstance.whatever();

This is simple, but it might be enough for you.

这很简单,但对你来说可能已经足够了。

If you really want to use the singleton pattern, look herefor a Java example. The advantage of this approach is that it makes sure that you only have 1 classA instance.

如果您真的想使用单例模式,请在此处查看 Java 示例。这种方法的优点是它确保您只有 1 个 classA 实例。

回答by LJ2

To elaborate on other answers:

详细说明其他答案:

public class A
{
private static A instance;
private A()
{
    ...
}

public static A getInstance()
{
    if (instance == null)
        instance = new A();

    return instance;
}
...
public void doSomething()
{
    ... 
}
}

And then call in B like this:

然后像这样调用 B:

A.getInstance().doSomething();

回答by dtyler

It sounds like you may want A to be a Singleton.

听起来您可能希望 A 成为Singleton

In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object.

在软件工程中,单例模式是一种设计模式,用于通过将类的实例化限制为一个对象来实现单例的数学概念。

This means your existing code will have to modified to use some kind of getInstance()method, but once that change has been made all instances of class B will use a single instance of class A.

这意味着您现有的代码将不得不修改以使用某种getInstance()方法,但是一旦进行了更改,类 B 的所有实例都将使用类 A 的单个实例。

Here is a direct link to the: simplest example from the above site.

这是指向上述站点的最简单示例的直接链接

回答by CamelSlack

Just use a static class and make sure it's public.

只需使用静态类并确保它是公共的。

回答by Vikas Chowdhury

Though you can use singleton design pattern to share the same instance every-time but if you want to use a same instance with parameters(overloaded constructor)..you cannot use SINGLETON

虽然您可以使用单例设计模式每次都共享同一个实例,但是如果您想使用带参数的同一个实例(重载构造函数)..你不能使用 SINGLETON