码迷,mamicode.com
首页 > 其他好文 > 详细

模式的秘密:单例模式

时间:2018-03-02 17:18:06      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:get   模式   int   stat   out   body   else   post   log   

public class Singleton {
    
    private Singleton ();
    
    private static Singleton Instance =new Singleton ();

    public static   Singleton getInstance (){
    
        return Instance ;
    }
           
}
public class Test{
    public static void main(String[] args){
    Singleton s1 = Singleton.getInstance();
    Singleton s2 = Singleton.getInstance();
    if(s1==s2){
        Systom.out.println("s1和s2是同一实例")
       }else{
        Systom.out.println("s1和s2不是同一实例")
        }
    }


}

  

  以上是饿汉模式

模式的秘密:单例模式

标签:get   模式   int   stat   out   body   else   post   log   

原文地址:https://www.cnblogs.com/luffe/p/8494081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!