实现单例模式

以后再补充吧
1. 饿汉式单例类  
public class SingletonClass {  
    private static final SingletonClass instance=new SingletonClass();  
//私有构造函数  
    private SingletonClass() {}  
    public static SingletonClass getInstance() {  
        return instance;  
    }  
}  
2. 懒汉式单例模式  
public class SingletonClass {  
    private static SingletonClass instance=null;  
//私有构造函数  
    private SingletonClass() {}  
    public synchronized static SingletonClass getInstance() {  
        if(instance==null) {  
            instance=new SingletonClass();  
        }  
        return instance;  
    }  
}   

你可能感兴趣的:(JAVA,数据结构与算法,Java笔试,剑指offer,面试,总结)