首页 > 其他 > 详细

单例类

时间:2018-08-03 13:34:25      阅读:198      评论:0      收藏:0      [点我收藏+]

如果一个类始终只能创建一个实例,

 

 1 class Singleton {
 2     public static Singleton instance;
 3     private Singleton() {}
 4     public static Singleton getInstance() {
 5         if (instance == null) {
 6             instance = new Singleton();
 7         }
 8 
 9         return instance;
10     }
11 }
12 
13 public class SingletonTest {
14     public static void main(String[] args) {
15         Singleton s1 = Singleton.getInstance();
16         Singleton s2 = Singleton.getInstance();
17 
18         System.out.println(s1 == s2);
19     }
20 }

使用单例类模式

 

单例类

原文:https://www.cnblogs.com/hmy-blog/p/9413341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!