class Single//饿汉式 调用时直接时初始化
{
private Single(){}//将构造方法私有化
private static Single s = new Single();
public static Single getInstance()
{
return s;
}
}
class Single//懒汉式
{
private Single(){}
private static Single s = null;
public static Single getInstance()//在调用静态方法时才去实例化,较饿汉式就是延迟了一些
{
if (s==null)
{
s=new Single();
}
return s;
}
} |