using System;
using System.Collections.Generic;
using System.Text;namespace Ex12_2
{
    class Program
    {
        static void Main(string[] args)
        {
            MyGenericClass<Person> myList = new MyGenericClass<Person>();
            myList.People.Add(new Student("蓝天"));
            myList.People.Add(new Employee("长江"));
            myList.People.Add(new Student("白云"));
            Console.WriteLine("myList EatFood");
            myList.EatFood();
            MyGenericClass<Student> studentList = myList.getStudent();
            Console.WriteLine("\nstudentList EatFood");
            studentList.EatFood();
            MyGenericClass<Person> myList2 = new MyGenericClass<Person>();
            myList2.People.Add(new Employee("黄河"));
            Console.WriteLine("\nmyList2 EatFood");
            myList2.EatFood();
            myList2 = myList2 + studentList;
            Console.WriteLine("\nNew myList2 EatFood");
            myList2.EatFood();
            MyGenericClass<Employee> e = myList.GetSpecies<Employee>();
            e.EatFood();
            Console.ReadKey();
        }
    }
}

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace Ex12_2
    {
        public abstract class Person
        {
            protected string name;        public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }        public Person()
            {
                name = "Default Name";
            }        public Person(string strName)
            {
                name = strName;
            }
            public void EatFood()
            {
                Console.WriteLine("Person {0} Eat Food.", name);
            }    }}
    using System;
    using System.Collections.Generic;
    using System.Text;namespace Ex12_2
    {
        public class Student :Person
        {
            public void Study()
            {
                Console.WriteLine("Student {0} Study.", name);
            }        public Student(string strName)
                : base(strName)
            {
            }
        }}
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;namespace Ex12_2
    {
        class MyGenericClass<T> : IEnumerable<T> where T : Person
        {
            private List<T> people;
            public MyGenericClass()
            {
                people = new List<T>();
            }
            public List<T> People
            {
                get { return people; }
            }
            public IEnumerator<T> GetEnumerator()
            {
                return people.GetEnumerator();
            }
            IEnumerator IEnumerable.GetEnumerator()
            {
                return people.GetEnumerator();
            }
            public static implicit operator MyGenericClass<Person>(MyGenericClass<T> Person)
            {
                MyGenericClass<Person> ret = new MyGenericClass<Person>();
                foreach (T t in Person)
                    ret.People.Add(t);
                return ret;
            }
            public static MyGenericClass<T> operator +(MyGenericClass<T> p1, MyGenericClass<T> p2)
            {
                MyGenericClass<T> result = new MyGenericClass<T>();
                foreach (T t in p1)
                {
                    result.People.Add(t);
                }
                foreach (T t in p2)
                {
                    if (!result.People.Contains(t))
                    {
                        result.People.Add(t);
                    }
                }
                return result;
            }
            public MyGenericClass<Student> getStudent()
            {
                MyGenericClass<Student> ret = new MyGenericClass<Student>();
                foreach(Person t in people)
                {
                    if (t is Student)
                        ret.People.Add((Student)t);
                }
                return ret;
            }
            public void EatFood()
            {
                foreach (T t in people)
                    t.EatFood();
            }
            public MyGenericClass<U> GetSpecies<U>() where U : T
            {
                MyGenericClass<U> species = new MyGenericClass<U>();
                foreach (T t in people)
                {
                    if (t is U)
                    {
                        species.People.Add(t as U);
                    }
                }
                return species;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;namespace Ex12_2
    {
        class Employee :Person
        {
            public void doWork()
            {
                Console.WriteLine("Employee {0} is working.", name);
            }        public Employee(string strName) : base(strName)
            {
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;namespace Ex12_2
    {
        class Program
        {
            static void Main(string[] args)
            {
                MyGenericClass<Person> myList = new MyGenericClass<Person>();
                myList.People.Add(new Student("蓝天"));
                myList.People.Add(new Employee("长江"));
                myList.People.Add(new Student("白云"));
                Console.WriteLine("myList EatFood");
                myList.EatFood();
                MyGenericClass<Student> studentList = myList.getStudent();
                Console.WriteLine("\nstudentList EatFood");
                studentList.EatFood();
                MyGenericClass<Person> myList2 = new MyGenericClass<Person>();
                myList2.People.Add(new Employee("黄河"));
                Console.WriteLine("\nmyList2 EatFood");
                myList2.EatFood();
                myList2 = myList2 + studentList;
                Console.WriteLine("\nNew myList2 EatFood");
                myList2.EatFood();
                MyGenericClass<Employee> e = myList.GetSpecies<Employee>();
                e.EatFood();
                Console.ReadKey();
            }
        }
    }
      

  2.   

       public IEnumerator<T> GetEnumerator()
       {
       return people.GetEnumerator();
       }
       IEnumerator IEnumerable.GetEnumerator()
       {
       return people.GetEnumerator();
       }
       public static implicit operator MyGenericClass<Person>(MyGenericClass<T> Person)
       就是上面几段代码  其实和泛型没什么关系  但是这个是泛型综合的例子