标签:
package 单例模式; /** * 单例设计模式之安全的懒汉式 */ class Single{ private static Single single= null; //私有构造方法 private Single() { } //对外提供公有的获取内部创建的Single对象 public static Single getInstance(){ if(single==null) { synchronized(Single.class) { if(single==null) { single = new Single(); } } } return single; } } public class Test { public static void main(String[] args) { Single s1 = Single.getInstance(); Single s2 = Single.getInstance(); System.out.println(s1); System.out.println(s2); System.out.println("==比较s1和s2两个对象:"+(s1==s2)); System.out.println("equals比较s1和s2两个对象:"+(s1.equals(s2))); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u012110719/article/details/46907213