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

本文探讨了如何在C#中创建Person类的集合,重载比较运算符以根据Age属性比较实例,并实现GetOldest()方法以获取Age最大的对象数组。此外,还详细介绍了如何在集合类上实现ICloneable接口,从而提供深度复制功能。
摘要由CSDN通过智能技术生成

   有一个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
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值