一:饿汉式:
1、声明一个私有的静态的最终的本类类型的对象并实例化:
private static final Person instance=new Person();
2、构造函数私有化:
private person(){};
3、通过公有的静态方法返回第一步实例化好的对象:
public static Person getInstance(){
return instance;
}
二、懒汉式:
1、声明一个私有的静态的本类类型的对象:
private static Person instance;
2、构造函数私有化:
private Person(){};
3、通过公有的静态方法返回第一步实例化好的对象:
public static Person getInstance(){
if(instance==null){
instance=new Person();
}
return instance;
}