using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
///
/// 泛型单例牺牲了构造函数的私有行
///
///
public class Singleton where T:new()
{
private static T singleton;
public Singleton()
{
}
public static T GetSingleton
{
get
{
if (singleton==null)
{
singleton = new T();
}
return singleton;
}
}
}
示例:
public class Student:Singleton
{
public void PintWorld()
{
Console.WriteLine(“I am a Student”);
}
}
使用:
public void CreatStudent()
{
Student stu=Student.GetSingleton;
stu.PintWorld();
}
结果:
I am a Student
测试:
自己测试,哈哈哈
评语:
没有上锁(什么是锁?有什么用?见下篇)