Java 在单例类中调用方法的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22010744/
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
Best way to call a method inside a singleton class
提问by AndEngine
I have a singleton class
我有一个单身课程
public class Singleton {
static Singleton instance;
public static Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
Consider a publicfunction method()is defined inside the class Singleton.
考虑在类Singleton 中定义了一个公共函数method()。
Which is the best way to call a method inside a singleton class:
这是在单例类中调用方法的最佳方式:
Singleton.method()- method calling statically
辛格尔顿。method()- 静态调用方法
or
或者
Singleton.getInstance.method() - method not static ?
Singleton.getInstance.method() - 方法不是静态的?
采纳答案by vipul mittal
In case of singleton classes there is no use of static methods as there is only one instance available of the class and every buddy is having the same copy of it.
在单例类的情况下,不使用静态方法,因为该类只有一个可用的实例,并且每个伙伴都拥有相同的副本。
so always create an instance method and call:
所以总是创建一个实例方法并调用:
Singleton.getInstance().method();
回答by Blackbelt
In the first case:
在第一种情况下:
Singleton.method();
method have to be static
方法必须是静态的
In the second case:
在第二种情况下:
Singleton.getInstance().method();
the method is not static.
该方法不是静态的。
So those are conceptually different
所以这些在概念上是不同的
回答by Adhikari Bishwash
Singleton.getInstance().method();
Singleton.getInstance().method();
is better as when called for first time instance will be null and instance is only created in getInstance().
更好,因为第一次调用时实例将为空并且实例仅在 getInstance() 中创建。
回答by Niko
If you want to use Singleton pattern:
如果你想使用单例模式:
public class Singleton {
private static Singleton sInstance;
public static Singleton getInstance() {
if (sInstance == null) {
sInstance = new Singleton();
}
return sInstance;
}
// Prevent duplicate objects
private Singleton() {
}
public void method() {
}
}
回答by zapl
The singleton pattern allows you to control the amount of instances that exist of one class - i.e. just 1. But the class itself is still a normal class that is not supposed to know how many of it's kind exist and it should therefore have normal instance methods.
单例模式允许你控制一个类存在的实例数量——即只有 1 个。但类本身仍然是一个普通的类,不应该知道它的种类有多少,因此它应该有普通的实例方法.
When using static
methods, you'll run into terrible problems if you ever want to change how many instances of that class exist.
使用static
方法时,如果您想更改该类存在的实例数量,您将遇到可怕的问题。
Either use a singleton or use static methods.
要么使用单例,要么使用静态方法。
回答by Sami Eltamawy
First, you should make sure that you have declared your Constructor
as a private one to prevent any one to call it and re-initialize it again. As following:
首先,您应该确保您已将您的声明声明Constructor
为私有的,以防止任何人调用它并再次重新初始化它。如下:
private void Singleton(){
//Initialize your private data
}
Second, call the static
methods directly as following:
其次,static
直接调用方法如下:
Singleton.yourMethod();
Third:, the non-static method calling as following:
第三:非静态方法调用如下:
Singleton.getInstance().yourMethod();
Here is a good Exampleof the Singleton
classes
这里是一个很好实施例的的Singleton
类
回答by abishek kachroo
Here you are having getInstance()
as a static method and you have created method
as a non-static or instance method.
在这里,您拥有getInstance()
一个静态方法,并且您创建 method
了一个非静态或实例方法。
So for a static method, Class.method_name
format can be used, but for an instance method object needs to be created.
所以对于静态方法,Class.method_name
可以使用格式,但是对于实例方法需要创建对象。
It may not be the correct syntax as shown over there:
它可能不是正确的语法,如那里所示:
Singleton.getInstance().method();
Singleton obj = new Singleton();
obj.method();
should be the correct format
应该是正确的格式