class Program
{
static void Main(string[] args)
{
SaySomeThing<Hi> saySomeThing_Hi = new SaySomeThing<Hi>();
Console.WriteLine(saySomeThing_Hi.GetObj().Content);
SaySomeThing<Hello> saySomeThing_Hello = new SaySomeThing<Hello>();
Hello hello = saySomeThing_Hello.GetObj();
Console.WriteLine(hello.Title + "\r\n" + hello.Content);
Console.ReadKey();
}
}
class SaySomeThing<T> where T : Hi
{
public T GetObj()
{
return Activator.CreateInstance<T>();
}
}
class Hi
{
public string Content { get; set; } = "窗前明月光,疑是地上霜。";
}
class Hello : Hi
{
public string Title { get; set; } = "静夜思";
}
输出:
窗前明月光,疑是地上霜。
静夜思
窗前明月光,疑是地上霜。