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
How to get specific instance of class from another class in Java?
提问by Edward Ruchevits
I've created the following class with the main
method, which creates new instance of Application
and instances of ApplicationModel
, ApplicationView
and ApplicationController
for this particular Application
.
我已经使用main
方法创建了以下类,该方法创建了Application
和 的新实例ApplicationModel
,ApplicationView
并ApplicationController
针对这个特定的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
, ApplicationView
and ApplicationController
classes.
我想在我的ApplicationModel
,ApplicationView
和ApplicationController
类中获得这个特定的实例。
How is it possible?
这怎么可能?
回答by Pablo Santa Cruz
I would use a singleton on Application class.
我会在 Application 类上使用单例。
Put a public static
method 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 Application
instance, 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 实例并且您需要这些类可以访问它,则您希望使用单例设计模式。我不会评论这是否是构建应用程序的最佳方式,但它会满足您问题中的要求。
回答by maba
Change the constructor of your Application
class to be private
and send the this
object to the ApplicationXxx
classes.
将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 new
on the Application
class from within the main
method.
您将能够调用new
在Application
从内类main
方法。
Your ApplicationSettings
, ApplicationModel
, ApplicationView
and ApplicationController
must be updated to take an Application
object in their constructors.
您的ApplicationSettings
, ApplicationModel
,ApplicationView
和ApplicationController
必须更新以Application
在其构造函数中接受一个对象。
回答by Roman C
Add parameter of the Application
type 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 this
as a constructor arg within the constructor of Application
to other classes (assuming the have a reference declared for the main class). That way you can obviously make multiple instances of the Application
class. If that is what you want...
我现在就尝试过,您可以将其this
作为构造函数中的构造函数 arg传递Application
给其他类(假设为主类声明了一个引用)。这样你显然可以创建Application
类的多个实例。如果这就是你想要的......