用 C# 实现的单例可能是这样的:
public class Singleton
{
private static Singleton instance;
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
如果我使用静态来实现它,例如:
public static class Globals{
public static Singleton Instance = new Singleton();
}
这样,app 也应该只获取整个应用程序的一个实例。那么这两种方法有什么区别呢?为什么不直接使用静态成员(更简单直接)?