首先定义一个基类:
class Father<T> where T : new()
{
private static T instance = new T();
//属性
public static T Instance => instance;
}
其次定义一个子类继承这个基类:
class Son : Father<Son>
{
public int data;
}
这样就能轻松get值啦~
class Program
{
static void Main(string[] args)
{
Son.Instance.data = 10;
}
}
版权声明:本文为wanghexuan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。