有一个Person类,代码如下:
InBlock.gifpublic class Person
InBlock.gif{
InBlock.gif                private string name;
InBlock.gif                private int age;
InBlock.gif                public string Name
InBlock.gif                {
InBlock.gif                        get
InBlock.gif                        { return name; }
InBlock.gif                        set
InBlock.gif                        { name = value; }
InBlock.gif                }
InBlock.gif                public int Age
InBlock.gif                {
InBlock.gif                        get
InBlock.gif                        {return age; }
InBlock.gif                        set
InBlock.gif                        {age=value;}
InBlock.gif                }
编写程序完成以下功能:
1)创建Person类的集合类people,该集合可以通过int型的索引符来访问.
2)在Person中重载>,<,比较Person实例的Age属性
3)给people添加GetOldest()方法,使用上面定义的重载运算符,返回一个Age最大的对象数组
4)在people类上执行ICloneable接口,提供深度复制功能
InBlock.gifusing System;
InBlock.gifusing System.Collections;                //Collections类用于使用对象数组
InBlock.gifusing System.Collections.Generic;
InBlock.gifusing System.Text;
InBlock.gif
namespace pro11
InBlock.gif{
InBlock.gif        public class Person : ICloneable    //使用ICloneable接口
InBlock.gif        {
InBlock.gif                private string name;
InBlock.gif                private int age;
InBlock.gif                public string Name
InBlock.gif                {
InBlock.gif                        get
InBlock.gif                        { return name; }
InBlock.gif                        set
InBlock.gif                        { name = value; }
InBlock.gif                }
InBlock.gif                public int Age
InBlock.gif                {
InBlock.gif                        get
InBlock.gif                        { return age; }
InBlock.gif                        set
InBlock.gif                        { age = value; }
InBlock.gif                }
InBlock.gif                public Person(string n, int a)        //构造函数
InBlock.gif                {
InBlock.gif                        Name = n;
InBlock.gif                        Age = a;
InBlock.gif                }
InBlock.gif                public object Clone()     //实现浅度复制
InBlock.gif                { return MemberwiseClone(); }
InBlock.gif                public static bool operator >(Person op1, Person op2)                 //对运算符>,<进行重载
InBlock.gif                { return (op1.Age > op2.Age); }
InBlock.gif
                public static bool operator <(Person op1, Person op2)
InBlock.gif                { return !(op1.Age > op2.Age); }
InBlock.gif
        }
InBlock.gif        public class people : CollectionBase, ICloneable        //继承CollectionBase类以使用对象数组
InBlock.gif        {
InBlock.gif                public void Add(Person newperson)     //Add方法用于添加对象成员
InBlock.gif                {
InBlock.gif                        List.Add(newperson);
InBlock.gif                }
InBlock.gif                public void Remove(Person oldperson)     //Remove方法用于删除对象成员
InBlock.gif                {
InBlock.gif                        List.Remove(oldperson);
InBlock.gif                }
InBlock.gif                public people()
InBlock.gif                { }
InBlock.gif                public Person this[int cardindex]        //为对象数组建立索引,使其可以以<对象名>[索引符]的方式进行引用
InBlock.gif                {
InBlock.gif                        get
InBlock.gif                        { return (Person)List[cardindex]; }
InBlock.gif                        set
InBlock.gif                        { List[cardindex] = value; }
InBlock.gif                }
InBlock.gif                public people GetOldest(people t)     //本人编写的方法,作用是返回一个对象数组,该数组成员是实参数组里面年龄最大的成员
InBlock.gif                {
InBlock.gif                        int oldest = 0;
InBlock.gif                        foreach (Person i in t)
InBlock.gif                        {
InBlock.gif                                if (i.Age > oldest)
InBlock.gif                                        oldest = i.Age;
InBlock.gif                        }
InBlock.gif                        people old = new people();
InBlock.gif                        foreach (Person j in t)
InBlock.gif                        {
InBlock.gif                                if (j.Age == oldest)
InBlock.gif                                        old.Add(new Person(j.Name, j.Age));
InBlock.gif                        }
InBlock.gif                        return old;
InBlock.gif                }
InBlock.gif                public object Clone()                        //深度复制的方法
InBlock.gif                {
InBlock.gif                        people newmen = new people();
InBlock.gif                        foreach (Person u in List)
InBlock.gif                        {
InBlock.gif                                newmen.Add(u.Clone() as Person);
InBlock.gif                        }
InBlock.gif                        return newmen;
InBlock.gif                }
InBlock.gif        }
InBlock.gif
        class Program
InBlock.gif        {
InBlock.gif                static void Main(string[] args)
InBlock.gif                {
InBlock.gif                        people my = new people();
InBlock.gif                        my.Add(new Person("jack", 56));
InBlock.gif                        my.Add(new Person("fat", 78));
InBlock.gif                        my.Add(new Person("rose", 43));
InBlock.gif                        my.Add(new Person("MC", 78));
InBlock.gif
                        people you = (people)my.Clone();     //由于这里使用了深度复制,所以下面修改you[1].Age的值不会影响my[1].Age的值,所以输出结果为"fat MC"
InBlock.gif                        you[1].Age = 45;
InBlock.gif                        people u = new people();
InBlock.gif                        u = u.GetOldest(my);
InBlock.gif
                        foreach (Person e in u)
InBlock.gif                        { Console.WriteLine(e.Name); }     //输出年龄最大的人的姓名
InBlock.gif                        Console.ReadKey();
InBlock.gif

InBlock.gif                }
InBlock.gif        }
InBlock.gif
}