1.深拷贝与浅拷贝
拷贝即是通常所说的复制(Copy)或克隆(Clone),对象的拷贝也就是从现有对象复制一个“一模一样”的新对象出来。虽然都是复制对象,但是不同的复制方法,复制出来的新对象却并非完全一模一样,对象内部存在着一些差异。通常的拷贝方法有两种,即深拷贝和浅拷贝,那二者之间有何区别呢?MSDN里对IClone接口的Clone方法有这样的说明:在深层副本中,所有的对象都是重复的;而在浅表副本中,只有顶级对象是重复的,并且顶级以下的对象包含引用。可以看出,深拷贝和浅拷贝之间的区别在于是否复制了子对象。这如何理解呢?下面我通过带有子对象的代码来验证二者的区别。
首先定义两个类型:Student和ClassRoom,其中Student类型里包含ClassRoom,并使这两个类型都分别实现自定义的深拷贝接口(IDeepCopy)和浅拷贝接口(IShallowCopy)。
类图如下:
定义代码如下:
定义代码
///<summary>/// 深拷贝接口 ///</summary>interface IDeepCopy { object DeepCopy(); }
///<summary>/// 浅拷贝接口 ///</summary>interface IShallowCopy { object ShallowCopy(); }
///<summary>/// 教室信息 ///</summary>class ClassRoom : IDeepCopy, IShallowCopy { publicint RoomID =1; publicstring RoomName ="Room1";
publicoverridestring ToString() { return"RoomID="+ RoomID +"\tRoomName="+ RoomName; } publicobject DeepCopy() { ClassRoom r =new ClassRoom(); r.RoomID =this.RoomID; r.RoomName =this.RoomName; return r; } publicobject ShallowCopy() { //直接使用内置的浅拷贝方法返回returnthis.MemberwiseClone(); } }
class Student : IDeepCopy, IShallowCopy { //为了简化,使用public 字段publicstring Name; publicint Age; //自定义类型,假设每个Student只拥有一个ClassRoompublic ClassRoom Room =new ClassRoom();
public Student() { } public Student(string name, int age) { this.Name = name; this.Age = age; } publicobject DeepCopy() { Student s =new Student(); s.Name =this.Name; s.Age =this.Age; s.Room = (ClassRoom)this.Room.DeepCopy(); return s; } publicobject ShallowCopy() { returnthis.MemberwiseClone(); }
publicoverridestring ToString() { return"Name:"+ Name +"\tAge:"+ Age +"\t"+ Room.ToString(); }
}
///<summary>/// 浅拷贝接口 ///</summary>interface IShallowCopy { object ShallowCopy(); }
///<summary>/// 教室信息 ///</summary>class ClassRoom : IDeepCopy, IShallowCopy { publicint RoomID =1; publicstring RoomName ="Room1";
publicoverridestring ToString() { return"RoomID="+ RoomID +"\tRoomName="+ RoomName; } publicobject DeepCopy() { ClassRoom r =new ClassRoom(); r.RoomID =this.RoomID; r.RoomName =this.RoomName; return r; } publicobject ShallowCopy() { //直接使用内置的浅拷贝方法返回returnthis.MemberwiseClone(); } }
class Student : IDeepCopy, IShallowCopy { //为了简化,使用public 字段publicstring Name; publicint Age; //自定义类型,假设每个Student只拥有一个ClassRoompublic ClassRoom Room =new ClassRoom();
public Student() { } public Student(string name, int age) { this.Name = name; this.Age = age; } publicobject DeepCopy() { Student s =new Student(); s.Name =this.Name; s.Age =this.Age; s.Room = (ClassRoom)this.Room.DeepCopy(); return s; } publicobject ShallowCopy() { returnthis.MemberwiseClone(); }
publicoverridestring ToString() { return"Name:"+ Name +"\tAge:"+ Age +"\t"+ Room.ToString(); }
}
测试代码:
测试代码
Student s1 =new Student("Vivi", 28); Console.WriteLine("s1=["+ s1 +"]"); Student s2 = (Student)s1.ShallowCopy(); //Student s2 = (Student)s1.DeepCopy(); Console.WriteLine("s2=["+ s2 +"]"); //此处s2和s1内容相同 Console.WriteLine("-----------------------------"); //修改s2的内容 s2.Name ="tianyue"; s2.Age =25; s2.Room.RoomID =2; s2.Room.RoomName ="Room2"; Console.WriteLine("s1=["+ s1 +"]"); Console.WriteLine("s2=["+ s2 +"]"); //再次打印两个对象以比较 Console.ReadLine();
运行结果:
a.ShallowCopy
s1=[Name:Vivi Age:28 RoomID=1 RoomName=Room1] s2=[Name:Vivi Age:28 RoomID=1 RoomName=Room1] -------------------------------------------------------------
s1=[Name:Vivi Age:28 RoomID=2 RoomName=Room2] s2=[Name:tianyue Age:25 RoomID=2 RoomName=Room2]
b.DeepCopy
s1=[Name:Vivi Age:28 RoomID=1 RoomName=Room1] s2=[Name:Vivi Age:28 RoomID=1 RoomName=Room1] ----------------------------- s1=[Name:Vivi Age:28 RoomID=1 RoomName=Room1] s2=[Name:tianyue Age:25 RoomID=2 RoomName=Room2]
从以上结果可以看出,深拷贝时两个对象是完全“分离”的,改变其中一个,不会影响到另一个对象;浅拷贝时两个对象并未完全“分离”,改变顶级对象的内容,不会对另一个对象产生影响,但改变子对象的内容,则两个对象同时被改变。这种差异的产生,即是取决于拷贝子对象时复制内存还是复制指针。深拷贝为子对象重新分配了一段内存空间,并复制其中的内容;浅拷贝仅仅将指针指向原来的子对象。
示意图如下:
2.浅拷贝与赋值操作
大多数面向对象语言中的赋值操作都是传递引用,即改变对象的指针地址,而并没有复制内存,也没有做任何复制操作。由此可知,浅拷贝与赋值操作的区别是顶级对象的复制与否。当然,也有一些例外情况,比如类型定义中重载赋值操作符(assignment operator),或者某些类型约定按值传递,就像C#中的结构体和枚举类型。
赋值操作示意图如下: