您的位置:首页 > 编程语言 > Java开发

Java单例模式简单实现

2014-02-13 13:53 176 查看
代码

public class Singleton {
private static Singleton singleton;//创建一个单例对象

public static Singleton getSingleton(){
if(singleton == null){//判断对象是否为空
singleton = new Singleton();
}
return singleton;
}
}


public class Test {
public static void main(String[] args) {
Singleton s1 = Singleton.getSingleton();
Singleton s2 = Singleton.getSingleton();

System.out.println(s1 == s2);            //输出true
System.out.println(s1.equals(s2));        //输出true
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: