为什么你不能做到以下几点?在类中而不是实例中初始化单例?
class foo {
private static foo instance = new foo();
List <string> messages = new List<string>();
private foo(){}
public static void doSomething(){..}
}
编辑:
我的意思是有没有这样做的差异:
class foo {
private static foo instance = new foo();
List <string> messages = new List<string>();
private foo(){}
public static void doSomething(){..}
}
或
class foo {
private static foo instance;
List <string> messages = new List<string>();
private foo(){}
public static void doSomething(){..}
public foo getInstance(){
if(instance!=null){
return instance;
}
}
}
你不能做什么?除了'doSomething'中缺少返回类型之外,这里一切都很好。 – spender