//懒汉式 刚开始不创建,要用才创建
class single2 //类加载进来,没有对象。只有调用类中的方法的时候,才会创建对象。
{ //单例设计模式的延迟加载形式。
private static single2 s= null;
private single2(){}
public static single2 getInstance()
{
if(s == null)
s = new single2();
return s;
}
}
//饿汉式
class single //类一加载出来,对象就存在了。
{
private single(){}
private static single s = new single();//①
public static single getInstance()
{
return s;
}
//上述三步,实现了对象的唯一性。
}
public class SingleDemo_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}