/**
* 对象是方法被调用时,才初始化,也叫做对象的延时加载,成为:懒汉式。
*
* */
public class SingleDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
/*
* 懒汉式:延迟加载
* Single类进内存 对象还没有存在 只有调用了GetInstance才建立对象
*
*记录原则:定义单例,开发时用饿汉式。
* */
class Single1{
private static Single1 s = null;
private Single1(){}
public static Single1 getInstance(){//可以加synchronized 方法加锁 多线程技术
if(s == null)
s = new Single1();
return s;
}
}