定义抽象的人类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1
{ public abstract class person
{
public abstract string ting(); public abstract string shuo();
}
}
定义子类 girl 和boy 类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1 {
public class girl : person
{
public override string ting() { return "girl ting"; }
public override string shuo() { return "girl shuo"; }
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1 {
public class boy: person
{
public override string ting() { return "boy ting"; }
public override string shuo() { return "boy shuo"; }
}
}
定义模具类可以 可以返回一个boy或者girl的实例
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1 {
public class zhongjian {
public static person CreatePerson<T>() where T:person
{
person p = Activator.CreateInstance(typeof(T)) as person; return p;
}
}
}
模拟环境类 program
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ person p= zhongjian.CreatePerson<girl>(); string shuo= p.shuo(); string ting= p.ting(); Console.WriteLine(shuo+"\t"+ting);
Console.ReadKey();
}
}
}