日期:2014-05-17 浏览次数:20833 次
public abstract class Animal { public abstract string AnimalType {get;} public override string ToString(){ return AnimalType; } //... //... //... } public class Cat:Animal { public override string AnimalType { get{ return "Cat";} } } public class Dog:Animal { public override string AnimalType { get{return "Dog";} } } .. .. .. public class EnterClass { public static void Main(string[] argv) { // ... //start foreach animal in animals Console.WriteLine(animal.ToString()); //end foreach } }
------解决方案--------------------
用泛型:
class AnimalBuilder { static public T Create<T>(T refobj) where T : new() { return new T(); } }