在继承中子类自动实现单例模式

@Paradox 技术支持

1
2
3
4
5
6
7
8
9
10
11
12
13
public abstract class Base<T> where T : new()
{
    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance == null)
                _instance = new T();
            return _instance;
        }
    }
}
1
2
3
public sealed class A : Base<A>
{
}

更多内容:http://www.liubaicai.net/index.php/archives/366

原文地址:https://www.cnblogs.com/liubaicai/p/4097244.html