class WorkExperence:ICloneable {
private string workDate;
private string company;
public string WorkDate {
set { workDate = value;}
get {return workDate;}
}
public string Company {
set { company = value; }
get { return company; }
}
public Object Clone() {
return (Object)this.MemberwiseClone();
}
}
//继承距ICloneable接口(system命名空间中)
class Resume:ICloneable
{
private string name;
private string age;
private string sex;
private WorkExperence work;
public Resume(string name) {
this.name = name;
work = new WorkExperence();
}
//实现对资源对象进行深复制的构造函数
public Resume(WorkExperence work) {
this.work = (WorkExperence)work.Clone();
}
public void SetPersonalInfo(string sex,string age){
this.sex = sex;
this.age = age;
}
public void SetWorkExperence(string workDate,string company) {
work.WorkDate = workDate;
work.Company = company;
}
public void Display() {
Console.WriteLine("{0} {1} {1}",name,sex,age);
Console.WriteLine("工作经历:{0} {1}",work.WorkDate,work.Company);
}
//浅复制
public Object Clone() {
return (Object)this.MemberwiseClone();
}
//深复制
public Object CloneH() {
//调用私有构造方法,让“工作经历”克隆完成,然后在给“简历”对象的相关字段赋值,最终返回一个深复制对象
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
}