单例设计模式

class Singleton{
    
    private static final Singleton INSTANCE = new Singleton(); //在类内部封装对象
    
    public static Singleton getInstance(){
        return INSTANCE;
    }
    
    private Singleton(){ //构造方法私有化
        
    }
    
    public void print(){
        System.out.println("HelloWorld!");
        
    }
}

public class helloworld {

    public static void main(String[] args) {
        Singleton inst = null;
        inst = Singleton.getInstance();
        inst.print();
        

    }

}

 

posted @ 2017-03-25 10:24  StanLong  阅读(90)  评论(0编辑  收藏  举报