制作类的全局实例 - JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18125106/
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
Make global instance of class - JAVA
提问by Marek Schubert
In this code, I am trying to make global instance of class so any methods can use it:
在这段代码中,我试图创建类的全局实例,以便任何方法都可以使用它:
public static void clip(){
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(character.gravity)character.y++; //there is an error, it says "Variable character does not exist"
Display.update();
Display.sync(70);
}
}
public static void initGame(){
entity character = new entity(); // I want to use this in method clip()
character.create(true, "box", true);
}
I've searched google, but on my question "Make instances of class global" found nothing. Thanks for help.
我搜索过谷歌,但在我的问题“使类的实例全局化”中一无所获。感谢帮助。
采纳答案by amit
There is no such thing as a "global variable" in java.
java中没有“全局变量”这样的东西。
However, you can declare character
as a class variableby declaring:
但是,您可以通过声明来声明character
为类变量:
entity character = new entity();
outside of methods, only on the class scope.
在方法之外,仅在类范围内。
Something like that:
类似的东西:
class MyClass {
private static entity character = new entity(); //class variable!
public static void clip(){
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(character.gravity)character.y++;
Display.update();
Display.sync(70);
}
}
public static void initGame(){
character = new entity(); // it will 'reset' game and bind a new object to the class variable `character`.
character.create(true, "box", true);
}
}
More info on class variables can be in the official documentation.
As a side node, a class should not be named entity
, since java has a strong convention that classes names should always start with upper case letter, you should rename this class to Entity
.
作为一个侧节点,一个类不应该被命名为entity
,因为 java 有一个强烈的约定,类名应该总是以大写字母开头,你应该将这个类重命名为Entity
.
回答by Lake
To make an instance of a particular class global to your program, one solution is the Singleton pattern. It works in the following way:
要使特定类的实例对您的程序具有全局性,一种解决方案是单例模式。它的工作方式如下:
class Entity {
private static Entity self = new Entity();
public static Entity get() { return self; }
}
You can then use the unique instance of class Entity from wherever in the code by:
然后,您可以通过以下方式从代码中的任何位置使用类 Entity 的唯一实例:
Entity.get()
回答by Antoniossss
Create singleton instance of your global class, and add static instance getter for it. Later on in any piece of your code you will be able to get your instance and call any public method of its class. Alternativly if you dont need singletons, simply use static method.
创建全局类的单例实例,并为其添加静态实例 getter。稍后在您的任何一段代码中,您都可以获取您的实例并调用其类的任何公共方法。或者,如果您不需要单身人士,只需使用静态方法。
回答by andy256
If you want one global object for a class, you can make a Singletonas follows.
如果你想要一个类的全局对象,你可以按如下方式创建一个单例。
class MyClass {
private MyClass instance = null;
public static MyClass getInstance() {
if (instance == null) {
instance = new MyClass();
}
return instance;
}
}
Now you can access the "global" instance:
现在您可以访问“全局”实例:
MyClass global = MyClass.getInstance();
回答by Shlublu
In Java, all objects should either be variables or members of a class, even though they are static members. C-style globals do not exist.
在 Java 中,所有对象都应该是变量或类的成员,即使它们是静态成员。C 风格的全局变量不存在。
So in your case, to make the character
accessible from everywhere, you should write:
因此,在您的情况下,要使character
可从任何地方访问,您应该编写:
public static entity CHARACTER = null;
public static void initGame(){
CHARACTER = new entity(); // I want to use this in method clip()
CHARACTER .create(true, "box", true);
}
And, provided the code above is from the class MyClass
, and the code below from another class:
并且,如果上面的代码来自 class MyClass
,而下面的代码来自另一个类:
public static void clip(){
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(MyClass.CHARACTER.gravity) MyClass.CHARACTER.y++;
Display.update();
Display.sync(70);
}
}
This being said, this code (its design actually) should be improved as it is absolutely not object oriented. As this is another question I won't elaborate further here but suggest that you investigate further in OOP standards and best practices.
话虽如此,这段代码(实际上是它的设计)应该改进,因为它绝对不是面向对象的。由于这是另一个问题,我不会在此详细说明,但建议您进一步研究 OOP 标准和最佳实践。