单例模式两种书写方式

两种都是静态的

1、

public class Single{
private Single(){ 立即加载
private static Single s= new Single();
public static Single getSingle(){
return s;
}
}

2、

public class Single{
private Single(){ } 延迟加载
private static class MySingle{
private static Single s = new Single();
}
public static Single getSingle(){
return MySingle.s;
}

猜你喜欢

转载自www.cnblogs.com/lh-lh/p/11838938.html