随笔- 129  评论- 0  文章- 0 

正确实现浅拷贝和深拷贝

  浅拷贝和深拷贝的区别:
  浅拷贝:
  修改副本的值类型字段不会影响源对象对应的字段,修改副本的引用类型字段会影响源对象,因为源对象复制给副本对象的时候,是引用类型的引用地址,也就是两者引用的是同一个对象。
  深拷贝:
  无论值类型还是引用类型的字段,修改副本对象不会影响源对象,即使是引用类型,也是重新创建了一个新的对象引用。
  要想自定义类型具有Clone拷贝的能力,就得继承ICloneable接口,然后根据需求,实现Clone方法以便实现浅拷贝或者深拷贝。
  浅拷贝示例:
  namespace WebApplication
  {
  public class Employee : ICloneable
  {
  public string IDCode { get; set; }
  public int Age { get; set; }
  public Department Department { get; set; }
  //实现ICloneable接口成员
  public object Clone()
  {
  return this.MemberwiseClone();
  }
  }
  public class Department
  {
  public string Name { get; set; }
  public override string ToString()
  {
  return this.Name;
  }
  }
  public partial class WebForm1 : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  //初始化Employee对象employeeA
  Employee employeeA = new Employee() { IDCode = "A", Age = 10, Department = new Department() { Name = "DepartmentA" } };
  //从employeeA 浅拷贝出 employeeB
  Employee employeeB = employeeA.Clone() as Employee;
  //修改employeeB对象的属性
  employeeA.IDCode = "B";
  employeeA.Age = 15;
  employeeA.Department.Name = "DepartmentB";
  //输出以便验证
  Response.Write(employeeB.IDCode); // A
  Response.Write(employeeB.Age); //10
  Response.Write(employeeB.Department.ToString()); //DepartmentB
  }
  }
  }
  从输出结果可以验证得到结果:
  1、IDCode即使是string引用类型,Object.MemberwiseClone 依然为其创造了副本,在浅拷贝中,我们可以将string当做值类型来看待。
  2、Employee的Department属性是引用类型,改变源对象employeeA中的值,会影响到副本对象employeeB
  浅拷贝和深拷贝的区别托福答案 www.yztrans.com
  浅拷贝:
  修改副本的值类型字段不会影响源对象对应的字段,修改副本的引用类型字段会影响源对象,因为源对象复制给副本对象的时候,是引用类型的引用地址,也就是两者引用的是同一个对象。
  深拷贝:
  无论值类型还是引用类型的字段,修改副本对象不会影响源对象,即使是引用类型,也是重新创建了一个新的对象引用。
  要想自定义类型具有Clone拷贝的能力,就得继承ICloneable接口,然后根据需求,实现Clone方法以便实现浅拷贝或者深拷贝。
  浅拷贝示例:
  namespace WebApplication
  {
  public class Employee : ICloneable
  {
  public string IDCode { get; set; }
  public int Age { get; set; }
  public Department Department { get; set; }
  //实现ICloneable接口成员
  public object Clone()
  {
  return this.MemberwiseClone();
  }
  }
  public class Department
  {
  public string Name { get; set; }
  public override string ToString()
  {
  return this.Name;
  }
  }
  public partial class WebForm1 : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  //初始化Employee对象employeeA
  Employee employeeA = new Employee() { IDCode = "A", Age = 10, Department = new Department() { Name = "DepartmentA" } };
  //从employeeA 浅拷贝出 employeeB
  Employee employeeB = employeeA.Clone() as Employee;
  //修改employeeB对象的属性
  employeeA.IDCode = "B";
  employeeA.Age = 15;
  employeeA.Department.Name = "DepartmentB";
  //输出以便验证
  Response.Write(employeeB.IDCode); // A
  Response.Write(employeeB.Age); //10
  Response.Write(employeeB.Department.ToString()); //DepartmentB
  }
  }
  }
  从输出结果可以验证得到结果:
  1、IDCode即使是string引用类型,Object.MemberwiseClone 依然为其创造了副本,在浅拷贝中,我们可以将string当做值类型来看待。
  2、Employee的Department属性是引用类型,改变源对象employeeA中的值,会影响到副本对象employeeBSAT答案 www.yzyxedu.com
 

posted on 2014-04-15 23:19  haosola  阅读(...)  评论(...编辑  收藏

toeflacttoeflieltstoefltoeflact