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();
}
}
}
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();
}
}
}
- C#:浅谈对象数组,运算符重载和深度复制的应用
- C#:浅谈对象数组,运算符重载和深度复制的应用
- 对象隐式转换,复制构造函数和重载等号运算符的区别
- 【C++】复制构造函数(拷贝构造函数)和=运算符重载使用的区别
- 浅谈JavaScript空数组[]和空对象{}的布尔运算
- 复制构造函数和=运算符重载的区别
- 【C++】重载运算符和重载函数
- []运算符的重载和二维数组类的[][]运算符重载
- C++:重载运算符避免数组越界
- 对象和数组的复制
- c++-运算符的重载
- C++--运算符的重载
- 一段代码引起的对C++构造函数、析构函数,复制构造函数,运算符重载的深度理解
- 对象的深度复制
- 对象的深度复制
- C++运算符重载浅谈
- 关于对象的深度复制和浅度复制
- [C/C++]运算符的重载
- 考研数学一近10年考题路线图1
- 考研数学一近10年考题路线图2
- 考研数学一近10年考题路线图3
- javaFx 学习--之组件篇1(边框与布局管理1)
- 信息检索趋势系列 人人网&千橡&分类网站(赛迪网独家评论)
- C#:浅谈对象数组,运算符重载和深度复制的应用
- OO思想
- .Grove—— .Net下的ORM框架
- oracle联机重做日志文件丢失
- 转一段关于hex文件格式的文章
- Linux下的多线程编程(转)
- Symbian中的iScanCode和iCode
- 正则表达式高级学习技巧
- 第三部分 类型设计2------《Microsoft.NET框架程序设计》