java 如何从Java中的另一个类获取类的特定实例?

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

How to get specific instance of class from another class in Java?

javaoopmodel-view-controllerclassinstance

提问by Edward Ruchevits

I've created the following class with the mainmethod, which creates new instance of Applicationand instances of ApplicationModel, ApplicationViewand ApplicationControllerfor this particular Application.

我已经使用main方法创建了以下类,该方法创建了Application和 的新实例ApplicationModelApplicationViewApplicationController针对这个特定的Application.

public class Application
{

    // Variables

    private ApplicationSettings         settings;
    private ApplicationModel            model;
    private ApplicationView             view;
    private ApplicationController       controller;

    // Constructor

    public Application()
    {
        settings        = new ApplicationSettings();
        model           = new ApplicationModel();
        view            = new ApplicationView(model);
        controller      = new ApplicationController();
    }

    // Main method

    public static void main(String[] args)
    {
        Application application = new Application();
    }

    // Getters for settings, model, view, controller for instance of Application

}

I know, that there will always be only one unique instance of Application.

我知道,总是只有一个独特的Application.

And I want to get this particular instance in my ApplicationModel, ApplicationViewand ApplicationControllerclasses.

我想在我的ApplicationModel,ApplicationViewApplicationController类中获得这个特定的实例。

How is it possible?

这怎么可能?

回答by Pablo Santa Cruz

I would use a singleton on Application class.

我会在 Application 类上使用单例。

Put a public staticmethod to return your one and only application instance.

放置一种public static方法来返回您唯一的应用程序实例。

public class Application
{
    private Application() { } // make your constructor private, so the only war
                              // to access "application" is through singleton pattern

    private static Application _app;

    public static Application getSharedApplication() 
    {
        if (_app == null)
            _app = new Application();
        return _app;
    }
}

You can read more about singleton design patternhere.

您可以在此处阅读有关单例设计模式的更多信息。

Every time you need the one and only Applicationinstance, you do:

每次您需要一个且唯一的Application实例时,您都会执行以下操作:

Application app = Application.getSharedApplication();

回答by smcg

You want to use the Singleton design pattern if you need to guarantee there will only be one instance of Application and you need it to be accessible by those classes. I'm not going to comment on if it's the best way to build your application, but it will satisfy the requirements in your question.

如果您需要保证只有一个 Application 实例并且您需要这些类可以访问它,则您希望使用单例设计模式。我不会评论这是否是构建应用程序的最佳方式,但它会满足您问题中的要求。

Singleton design pattern tutorial

单例设计模式教程

回答by maba

Change the constructor of your Applicationclass to be privateand send the thisobject to the ApplicationXxxclasses.

Application类的构造函数更改为private并将this对象发送到ApplicationXxx类。

private Application()
{
    settings        = new ApplicationSettings(this);
    model           = new ApplicationModel(this);
    view            = new ApplicationView(this, model);
    controller      = new ApplicationController(this);
}

You will be able to call newon the Applicationclass from within the mainmethod.

您将能够调用newApplication从内类main方法。

Your ApplicationSettings, ApplicationModel, ApplicationViewand ApplicationControllermust be updated to take an Applicationobject in their constructors.

您的ApplicationSettings, ApplicationModel,ApplicationViewApplicationController必须更新以Application在其构造函数中接受一个对象。

回答by Roman C

Add parameter of the Applicationtype to the constructors of the composites. When you create their instances just pass this. For example

Application类型的参数添加到组合的构造函数中。当您创建他们的实例时,只需通过this. 例如

public class Application {
  String s = "Setting";
  class ApplicationSettings {
    Application sc;
    ApplicationSettings(Application sc){
      this.sc = sc;
      System.out.println(sc.s);
    }
  }

  // Variables

  private ApplicationSettings settings;

  // Constructor

  public Application()
  {
    settings = new ApplicationSettings(this);
  }

  // Main method

  public static void main(String[] args)
  {
    Application application = new Application();

  }

  // Getters for settings, model, view, controller for instance of Application

}

回答by Less

I tried this right now, you can pass thisas a constructor arg within the constructor of Applicationto other classes (assuming the have a reference declared for the main class). That way you can obviously make multiple instances of the Applicationclass. If that is what you want...

我现在就尝试过,您可以将其this作为构造函数中的构造函数 arg传递Application给其他类(假设为主类声明了一个引用)。这样你显然可以创建Application类的多个实例。如果这就是你想要的......