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)));}
}
转载于:https://www.cnblogs.com/wzzkaifa/p/6768900.html