引用类型直接拷贝:内存中存在两份引用,但是真实数据只有一份,两份引用指向同一份真实数据
浅拷贝:如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型(work),则复制引用但不复制引用的对象
深拷贝:完全生成不同的对象,各个对象里面的字段(不管是值类型还是引用类型)在内存中占据不同的位置
代码:
Code
public class Resume : ICloneable
{
private int age;
private string name;
private WorkExperice work;
public Resume()
{
work = new WorkExperice();
}
public void SetInfo(int age,string name,string company)
{
this.age = age;
this.name = name;
work.company = company;
}
public void Display()
{
Console.WriteLine("{0} : {1},{2}",name,age,work.company);
}
#region ICloneable Members
//浅拷贝
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
//深拷贝
public object Copy()
{
Resume res = new Resume();
res.age = this.age;
res.name = this.name;
res.work = (WorkExperice)work.Clone();
return res;
}
}
public class WorkExperice : ICloneable
{
public string company;
#region ICloneable Members
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
}
public class Resume : ICloneable
{
private int age;
private string name;
private WorkExperice work;
public Resume()
{
work = new WorkExperice();
}
public void SetInfo(int age,string name,string company)
{
this.age = age;
this.name = name;
work.company = company;
}
public void Display()
{
Console.WriteLine("{0} : {1},{2}",name,age,work.company);
}
#region ICloneable Members
//浅拷贝
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
//深拷贝
public object Copy()
{
Resume res = new Resume();
res.age = this.age;
res.name = this.name;
res.work = (WorkExperice)work.Clone();
return res;
}
}
public class WorkExperice : ICloneable
{
public string company;
#region ICloneable Members
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
}
客户端代码:
Code
#region 引用类型直接拷贝
//内存中存在两份引用,但是真实数据只有一份,两份引用指向同一份真实数据
Resume ResumeA = new Resume();
ResumeA.SetInfo(18,"EngineA","CompanyA");
Resume ResumeB = ResumeA;
ResumeB.SetInfo(19,"EngineB","CompanyB");
ResumeA.Display();
ResumeB.Display();
//expected result:
//EngineB : 19,CompanyB
//EngineB : 19,CompanyB
#endregion
#region 浅拷贝
//如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型(work),则复制引用但不复制引用的对象
Resume ResumeC = new Resume();
ResumeC.SetInfo(20,"EngineC","CompanyC");
Resume ResumeD = (Resume)ResumeC.Clone();
ResumeD.SetInfo(21,"EngineD","CompanyD");
ResumeC.Display();
ResumeD.Display();
//expected result :
//EngineC : 20,CompanyD
//EngineD : 21,CompanyD
#endregion
#region 深拷贝
//完全生成不同的对象,各个对象里面的字段(不管是值类型还是引用类型)在内存中占据不同的位置
Resume ResumeE = new Resume();
ResumeE.SetInfo(22,"EngineE","CompanyE");
Resume ResumeF = (Resume)ResumeE.Clone();
ResumeF.SetInfo(23,"EngineF","CompanyF");
ResumeE.Display();
ResumeF.Display();
//expected result :
//EngineE : 22,CompanyE
//EngineF : 23,CompanyF
#endregion
#region 引用类型直接拷贝
//内存中存在两份引用,但是真实数据只有一份,两份引用指向同一份真实数据
Resume ResumeA = new Resume();
ResumeA.SetInfo(18,"EngineA","CompanyA");
Resume ResumeB = ResumeA;
ResumeB.SetInfo(19,"EngineB","CompanyB");
ResumeA.Display();
ResumeB.Display();
//expected result:
//EngineB : 19,CompanyB
//EngineB : 19,CompanyB
#endregion
#region 浅拷贝
//如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型(work),则复制引用但不复制引用的对象
Resume ResumeC = new Resume();
ResumeC.SetInfo(20,"EngineC","CompanyC");
Resume ResumeD = (Resume)ResumeC.Clone();
ResumeD.SetInfo(21,"EngineD","CompanyD");
ResumeC.Display();
ResumeD.Display();
//expected result :
//EngineC : 20,CompanyD
//EngineD : 21,CompanyD
#endregion
#region 深拷贝
//完全生成不同的对象,各个对象里面的字段(不管是值类型还是引用类型)在内存中占据不同的位置
Resume ResumeE = new Resume();
ResumeE.SetInfo(22,"EngineE","CompanyE");
Resume ResumeF = (Resume)ResumeE.Clone();
ResumeF.SetInfo(23,"EngineF","CompanyF");
ResumeE.Display();
ResumeF.Display();
//expected result :
//EngineE : 22,CompanyE
//EngineF : 23,CompanyF
#endregion