#中有两种类型变量,一种是值类型变量,一种是引用类型变量。
对于前者,copy是属于全盘复制;而对于后者,一般的copy只是浅copy,只copy引用地址,相当于只传递一个引用指针一样。因此对于后者进行真正copy的时候,也是最费事的,具体的说,必须为其实现 ICloneable接口中提供的Clone方法,从而产生一个全新的对象。
浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用.
深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
浅拷贝和深拷贝之间的区别:
浅拷贝是指将对象中的数值类型的字段拷贝到新的对象中,而对象中的引用型字段则指复制它的一个引用到目标对象。如果改变目标对象中引用型字段的值他将反映在原始对象中,也就是说原始对象中对应的字段也会发生变化。
深拷贝与浅拷贝不同的是对于引用的处理,深拷贝将会在新对象中创建一个新的和原始对象中对应字段相同(内容相同)的字段,也就是说这个引用和原始对象的引用是不同的,我们在改变新对象中的这个字段的时候是不会影响到原始对象中对应字段的内容。
所以对于原型模式(prototype pattern)也有不同的两种处理方法:对象的浅拷贝和深拷贝
MemberwiseClone 方法创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。如果字段是值类型的,则对该字段执行逐位复制。如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。深拷贝,即实现ICloneable接口.ICloneable可用于深拷贝和浅拷贝。
.net提供了一个ICloneable接口,该接口下有一个Clone()方法,你可以实现它用来实现你自己的克隆方式,比如深克隆或是浅克隆,MemberwiseClone()是object类中的一个方法,用来实现类的浅克隆
using System.Collections.Generic; #region//深克隆 浅克隆 MemberwiseClone() clone() /// <summary> /// Abstract Class Animal /// </summary> public abstract class Animal { public int i; public double d; public byte b; public string[] s; public abstract Animal Clone(); } /// <summary> /// SubClass Dog /// </summary> public class Dog : Animal { public Dog(int i, double d, byte b, string s1, string s2) { this.i = i; this.d = d; this.b = b; string[] ms = { s1, s2 }; this.s = ms; } public override Animal Clone() { return (Animal)this.MemberwiseClone(); } } #endregion protected void Page_Load(object sender, EventArgs e) { #region//深克隆 浅克隆 MemberwiseClone() clone() Animal a1 = new Dog(1, 2, 3, "A", "B"); Response.Write("Animal a1 ‘s members :"+a1.i+" "+a1.d+" "+ a1.b+ a1.s[0]+ a1.s[1]+"<br/>"); Animal a2; a2 = a1.Clone(); Response.Write("Animal a2 ‘s members : "+ a2.i+ a2.d+ a2.b+ a2.s[0]+ a2.s[1]+"<br/>"); Response.Write("do a1.i = 9;a1.s[0] = C"+"<br/>"); a1.i = 9; a1.s[0] = "C"; Response.Write("Animal a1 ‘s members : "+a1.i+a1.d+ a1.b+ a1.s[0]+ a1.s[1]+"<br/>"); Response.Write("Animal a2 ‘s members : "+ a2.i+ a2.d+ a2.b+ a2.s[0]+ a2.s[1]+"<br/>"); Response.Write("do a2.i = 8;a2.s[1] =D" + "<br/>"); a2.i = 8; a2.s[1] = "D"; Response.Write("Animal a1 ‘s members : " + a1.i + a1.d + a1.b + a1.s[0] + a1.s[1] + "<br/>"); Response.Write("Animal a2 ‘s members : " + a2.i + a2.d + a2.b + a2.s[0] + a2.s[1] + "<br/>"); #endregion }
output
Animal a1 ‘s members :1 2 3ABAnimal a2 ‘s members : 123ABdo a1.i = 9;a1.s[0] = CAnimal a1 ‘s members : 923CBAnimal a2 ‘s members : 123CBdo a2.i = 8;a2.s[1] =DAnimal a1 ‘s members : 923CDAnimal a2 ‘s members : 823CD
如果用深克隆,虽然简单类可能只有值类型,但是指不定什么时候就有个引用类型。深克隆目前来说比较实在的方法是序列化加反序列化,当然也可以使用反射,或者一个对一个的赋值。一般是做一个方法比如叫DeepCopy().请看下面的例子:在Dog类中添加新方法 DeepClone(),同时Dog 类要实现[Serialize]
public Dog DeepClone() { BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); bFormatter.Serialize(stream, this); stream.Seek(0, SeekOrigin.Begin); return (Dog)bFormatter.Deserialize(stream); }
Page_Load中添加:
#region//深克隆 ICloneable接口下的 Clone()方法 Dog dog1 = new Dog(1, 2, 3, "A", "B"); Response.Write("Dog dog1 ‘s members :" + dog1.i + " " + dog1.d + " " + dog1.b + dog1.s[0] + dog1.s[1] + "<br/>"); Dog dog2 = dog1.DeepClone(); Response.Write("Dog dog2 ‘s members : " + dog2.i + dog2.d + dog2.b + dog2.s[0] + dog2.s[1] + "<br/>"); Response.Write("Change dog1.i = 9;dog1.s[0] = C" + "<br/>"); dog1.i = 9; dog1.s[0] = "C"; Response.Write("Dog dog1 ‘s members :" + dog1.i + " " + dog1.d + " " + dog1.b + dog1.s[0] + dog1.s[1] + "<br/>"); Response.Write("Dog dog2 ‘s members : " + dog2.i + dog2.d + dog2.b + dog2.s[0] + dog2.s[1] + "<br/>"); Response.Write("Change dog2.i = 8;dog2.s[1] = D" + "<br/>"); dog2.i = 8; dog2.s[1] = "D"; Response.Write("Dog dog1 ‘s members :" + dog1.i + " " + dog1.d + " " + dog1.b + dog1.s[0] + dog1.s[1] + "<br/>"); Response.Write("Dog dog2 ‘s members : " + dog2.i + dog2.d + dog2.b + dog2.s[0] + dog2.s[1] + "<br/>"); #endregion
output
Dog dog1 ‘s members :1 2 3ABDog dog2 ‘s members : 123ABChange dog1.i = 9;dog1.s[0] = CDog dog1 ‘s members :9 2 3CBDog dog2 ‘s members : 123ABChange dog2.i = 8;dog2.s[1] = DDog dog1 ‘s members :9 2 3CBDog dog2 ‘s members : 823AD