这段代码是怎么实现的深层拷贝?(code在里面)

崔鹏飞 2008-09-03 04:04:20
class Student : ICloneable
{
public string Name;
public Int32 Age;

public Student(string name, Int32 age)
{
Name = name;
Age = age;
}

public void ShowInfo()
{
Console.WriteLine("{0}'s age is {1}", Name, Age);
}

public Object Clone()
{
//实现浅拷贝
return MemberwiseClone();
}
}

class Enrollment : ICloneable
{
//定义引用类型数据成员
public List<Student> students = new List<Student>();

//定义默认构造函数
public Enrollment()
{
}

//提供实现深拷贝的私有实例创建构造函数
private Enrollment(List<Student> studentList)
{
foreach (Student s in studentList)
{
students.Add((Student)s.Clone());
}
}

public void ShowEnrollmentInfo()
{
Console.WriteLine("Students enrollment information:");
foreach (Student s in students)
{
Console.WriteLine("{0}'s age is {1}", s.Name, s.Age);
}
}

public object Clone()
{
//执行浅拷贝
//return MemberwiseClone();
//执行深拷贝
return new Enrollment(students);
}
}
...全文
516 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cuipengfei1 的回复:]
我在六楼的理解对吗??
[/Quote]
崔鹏飞 2008-09-04
  • 打赏
  • 举报
回复
请看一下六楼对吗?[Quote=引用 7 楼 cuipengfei1 的回复:]
我在六楼的理解对吗??
[/Quote]
孤剑 2008-09-04
  • 打赏
  • 举报
回复
9楼的兄弟说的比较详细了。

我在补充一点,如果还是不理解可以看看 DataTable.Clone() DataTable.Copy() 的差异。
直接从数据上看:
Clone() 后的table2继续保存原有table1的结构(structure)不变;
Copy() 后 table2 里填充的全部是table1 的内容,包括结构和数据(data)

同时,对于 MemberwiseClone() 的最好解释,还是看看MSDN吧》
MemberwiseClone cannot be overridden, and is only accessible through this class or a derived class. Use a class that implements the ICloneable interface if a deep or shallow copy of an object needs to be publicly exposed to users.

A shallow copy creates a new instance of the same type as the original object, and then copies the nonstatic fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object. In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.

For example, if X is an Object with references to the objects A and B, and the object A also has a reference to an object M, a shallow copy of X is an object Y, which also has references to objects A and B. In contrast, a deep copy of X is an object Y with direct references to objects C and D, and an indirect reference to object N, where C is a copy of A, D is a copy of B, and N is a copy of M.



hustcyb 2008-09-04
  • 打赏
  • 举报
回复
首先来看Student的Clone方法:

public Object Clone()
{
//实现浅拷贝
return MemberwiseClone();
}

这里关键是MemberwiseClone()方法,首先它会创建一个新的Student对象,接着为Student创建一个新的string类型的Name字段和int类型的Age字段,然后将原始Student对象中的Name中字符串值和Age的值复制过来。

这里要注意string虽然是引用类型,但是微软对其进行了特殊处理,复制时是复制其值而不是引用。因此上面的代码完全等同于:


public Object Clone()
{
return new Student(Name, Age);
}

也就是说Student的Clone是深复制而不是浅复制。
coolshark 2008-09-04
  • 打赏
  • 举报
回复
关注~~关注~~ C++影子好重
崔鹏飞 2008-09-03
  • 打赏
  • 举报
回复
我在六楼的理解对吗??
崔鹏飞 2008-09-03
  • 打赏
  • 举报
回复
是不是这样:
public object Clone()
{
return new Enrollment(students);
//这里传的参数是原来旧对象的students
}



private Enrollment(List<Student> studentList)
{
foreach (Student s in studentList)
{
students.Add((Student)s.Clone());
//在这个私有的构造函数里面写的students是新的实例的students
}
}


这样解释对吗??
崔鹏飞 2008-09-03
  • 打赏
  • 举报
回复
return new Enrollment(students);
[Quote=引用 2 楼 gomoku 的回复:]
就是把studentsstudentList里面已有的元素再复制一份还是放在students里面
[/Quote]但是调用的时候,还是把students传进去了啊......
TengGaoqing 2008-09-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jacklau88 的回复:]
foreach (Student s in studentList)
{
students.Add((Student)s.Clone());
}
[/Quote]
jacklau88 2008-09-03
  • 打赏
  • 举报
回复
foreach (Student s in studentList)
{
students.Add((Student)s.Clone());
}
gomoku 2008-09-03
  • 打赏
  • 举报
回复
就是把studentsstudentList里面已有的元素再复制一份还是放在students里面
崔鹏飞 2008-09-03
  • 打赏
  • 举报
回复
foreach (Student s in studentList)
{
students.Add((Student)s.Clone());
}

这样就是深拷贝了?
这不是就是把students里面已有的元素再复制一份还是放在students里面吗??

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧