1
//
一个单件模式的类
2 class singleton{
3 // 声明一个私有的单件类成员,初始化为空,延迟加载
4 private static singleton _instance = null ;
5
6 // 把单件类的构造函数声明为私有,从而禁止通过new来构造单件类的实例
8 private singleton()
9 {
10
11 }
12
13 // 对外公开的获取单件实例的方法
14 public static singleton GetInstance(){
15 if (_instance == null ){
16 _instance = new singleton();
17 }
18 return _instance; 1
20 }
21
22 }
2 class singleton{
3 // 声明一个私有的单件类成员,初始化为空,延迟加载
4 private static singleton _instance = null ;
5
6 // 把单件类的构造函数声明为私有,从而禁止通过new来构造单件类的实例
8 private singleton()
9 {
10
11 }
12
13 // 对外公开的获取单件实例的方法
14 public static singleton GetInstance(){
15 if (_instance == null ){
16 _instance = new singleton();
17 }
18 return _instance; 1
20 }
21
22 }