C#:浅谈对象数组,运算符重载和深度复制的应用

原文网址:http://dev.firnow.com/course/3_program/cshapo/csharpjs/20100108/188361_2.html

 

有一个Person类,代码如下:


public class Person
{
                private string name;
                private int age;
                public string Name
                {
                        get
                        { return name; }
                        set
                        { name = value; }
                }
                public int Age
                {
                        get
                        {return age; }
                        set
                        {age=value;}
                }
编写程序完成以下功能:
1)创建Person类的集合类people,该集合可以通过int型的索引符来访问.
2)在Person中重载>,<,比较Person实例的Age属性
3)给people添加GetOldest()方法,使用上面定义的重载运算符,返回一个Age最大的对象数组
4)在people类上执行ICloneable接口,提供深度复制功能


using System;
using System.Collections;                //Collections类用于使用对象数组
using System.Collections.Generic;
using System.Text;

namespace pro11
{
        public class Person : ICloneable    //使用ICloneable接口
        {
                private string name;
                private int age;
                public string Name
                {
                        get
                        { return name; }
                        set
                        { name = value; }
                }
                public int Age
                {
                        get
                        { return age; }
                        set
                        { age = value; }
                }
                public Person(string n, int a)        //构造函数
                {
                        Name = n;
                        Age = a;
                }
                public object Clone()     //实现浅度复制
                { return MemberwiseClone(); }
                public static bool operator >(Person op1, Person op2)                 //对运算符>,<进行重载
                { return (op1.Age > op2.Age); }

                public static bool operator <(Person op1, Person op2)
                { return !(op1.Age > op2.Age); }

        }


        public class people : CollectionBase, ICloneable        //继承CollectionBase类以使用对象数组
        {
                public void Add(Person newperson)     //Add方法用于添加对象成员
                {
                        List.Add(newperson);
                }
                public void Remove(Person oldperson)     //Remove方法用于删除对象成员
                {
                        List.Remove(oldperson);
                }
                public people()
                { }
                public Person this[int cardindex]        //为对象数组建立索引,使其可以以<对象名>[索引符]的方式进行引用
                {
                        get
                        { return (Person)List[cardindex]; }
                        set
                        { List[cardindex] = value; }
                }
                public people GetOldest(people t)     //本人编写的方法,作用是返回一个对象数组,该数组成员是实参数组里面年龄最大的成员
                {
                        int oldest = 0;
                        foreach (Person i in t)
                        {
                                if (i.Age > oldest)
                                        oldest = i.Age;
                        }
                        people old = new people();
                        foreach (Person j in t)
                        {
                                if (j.Age == oldest)
                                        old.Add(new Person(j.Name, j.Age));
                        }
                        return old;
                }
                public object Clone()                        //深度复制的方法
                {
                        people newmen = new people();
                        foreach (Person u in List)
                        { 
                              newmen.Add(u.Clone() as Person);
                        }
                        return newmen;
                }
        }

        class Program
        {
                static void Main(string[] args)
                {
                        people my = new people();
                        my.Add(new Person("jack", 56));
                        my.Add(new Person("fat", 78));
                        my.Add(new Person("rose", 43));
                        my.Add(new Person("MC", 78));

                        people you = (people)my.Clone();     //由于这里使用了深度复制,所以下面修改you[1].Age的值不会影响my[1].Age的值,所以输出结果为"fat MC"
                        you[1].Age = 45;
                        people u = new people();
                        u = u.GetOldest(my);

                        foreach (Person e in u)
                        { Console.WriteLine(e.Name); }     //输出年龄最大的人的姓名
                        Console.ReadKey();


                }
        }

}
文章出处:飞诺网(http://dev.firnow.com/course/3_program/cshapo/csharpjs/20100108/188361_3.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值