爲什麼你不能做到以下幾點?在類中而不是實例中初始化單例?
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