蜗牛的JAVA外壳

┎Running Snail┒ ┖ -------------- ┚

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  13 Posts :: 0 Stories :: 10 Comments :: 0 Trackbacks
定义:保证在Java程序中,一个Class只有一个实例存在。

第一种实例:
 1public class Singleton {
 2
 3  private Singleton(){}
 4
 5  private static Singleton instance = new Singleton();
 6
 7  public static Singleton getInstance() {
 8    return instance;   
 9   }
 
10}

11 
12


第二种实例:

 1public class Singleton 
 2
 3  private static Singleton instance = null;
 4
 5  public static synchronized Singleton getInstance() {
 6      if (instance==null)
 7        instance=new Singleton();
 8      return instance;   
 9    }
 
10
11}

12
posted on 2007-04-22 01:45 会跑的蜗牛 阅读(222) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: