单例模式
单例模式可以确保该类的对象有且只有一个
延迟加载
public class test {
//用static声明
private static test test;
//将构造方法私有化,这样就不可以随便new这个类了
private test(){
test=new test();
}
//用静态方法调用私有构造方法
public static test gettest(){
//如果等于null则调用私有的构造方法
//如果不等于null则不需要
if(test==null){
test=new test();
}
return test;
}
}
直接加载
public class test {
//用static声明同时直接new这样直接加载
private static test test=new test();
//将构造方法私有化,这样就不可以随便new这个类了
private test(){
test=new test();
}
//用静态方法调用私有构造方法
public static test gettest(){
//如果等于null则调用私有的构造方法
//如果不等于null则不需要
if(test==null){
test=new test();
}
return test;
}
}