//饿汉式
class Single//类一加载,对象就已经存在了。
private static Singele s= new Single();//new出该类的一个对象
private Single(){}//私有化构造方法
public static Single getInstance()
{
return s;
}
//懒汉式
class Single2//类加载进来,没有对象,只有调用了getInstance方法时,才会创建对象
//延迟加载形式。
{
private static Single2 s=null;
private Single2(){}
public static Single2 getInstance()
{
if(s==null)
s=new Single2();
return s;
}
}
转载于:https://my.oschina.net/mikezhaoweb/blog/295026