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

来源:互联网 发布:宝宝取名软件免费版 编辑:程序博客网 时间:2024/06/03 20:02

   有一个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();


        }

    }


}
     
原创粉丝点击