class WorkExperience : ICloneable
{
private string workDate;
private string company;
public Object Clone ( )
{
return ( Object) this . MemberwiseClone ( ) ;
}
public string WorkDate { get = > workDate; set = > workDate = value; }
public string Company { get = > company; set = > company = value; }
}
class Resume : ICloneable
{
private string name;
private string sex;
private string age;
private WorkExperience work;
public Resume ( string name)
{
this . name = name;
work = new WorkExperience ( ) ;
}
private Resume ( WorkExperience work)
{
this . work = ( WorkExperience) work. Clone ( ) ;
}
public void SetPersonalInfo ( string sex, string age)
{
this . sex = sex;
this . age = age;
}
public void SetWorkExperience ( string workDate, string company)
{
work. WorkDate = workDate;
work. Company = company;
}
public void Display ( )
{
Console. WriteLine ( $"{name} {sex} {age}" ) ;
Console. WriteLine ( $"工作经历:{work.WorkDate} {work.Company}" ) ;
}
public Object Clone ( )
{
Resume obj = new Resume ( this . work) ;
obj. name = this . name;
obj. sex = this . sex;
obj. sex = this . age;
return obj;
}
}
class Program
{
static void Main ( string[ ] args)
{
Resume r1 = new Resume ( "王小二" ) ;
r1. SetPersonalInfo ( "男" , "21" ) ;
r1. SetWorkExperience ( "2019.6.19" , "腾讯" ) ;
Resume r2 = ( Resume) r1. Clone ( ) ;
r2. SetWorkExperience ( "2019.7.21" , "腾讯" ) ;
r1. Display ( ) ;
r2. Display ( ) ;
}
}
深拷贝的应用。