在C#中的ICloneable中给出的Clone方法只是一个模棱两可的方法,并不足以实现深拷贝和浅拷贝。
深拷贝:
using(Stream objectStream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(objectStream, this); objectStream.Seek(0, SeekOrigin.Begin); return formatter.Deserialize(objectStream) as Test; }
使用using开出一段流,将对象序列化。这里没有成功,有兴趣的可以试下。
深拷贝与浅拷贝的根本区别就在于:是否拷贝的是值还是地址。
浅拷贝是两个对象指向同一片地址。
深拷贝是两个对象两个完全不同的地址,但是里面的内容一样。
我的思路是:A类中的字段是B类的对象。直接拷贝A类中的属性,属性是字段的话,重新申明一个B类对象,将A类中B的对象里的拷贝进去。
class Number { public string number { get; set; } public override string ToString() { return this.number; } public Number() { this.number = string.Empty; } public void NumberClone(Number num) { this.number = num.number; } }
在这里,属性不能自己调用自己。如果调用则会陷入死循环。
public Number CabinetNumber
{
get{ return CabinetNumber;}
set{ }
}
class Cabinet { public int No { get; set; } public int Side { get; set; } public string Name { get; set; } private Number _number; public Number CabinetNumber { //get; set; get { if (_number == null) { _number = new Number(); } return _number; } set { } } public void CreateCabinet(Cabinet cabinet) { this.No = cabinet.No; this.Side = cabinet.Side; this.Name = cabinet.Name; Console.WriteLine("this.GetHashCode: " + this.GetHashCode()); Console.WriteLine(); } public Cabinet CreateCabinetNumber(Cabinet cabinet) { //this.No = cabinet.No; //this.Side = cabinet.Side; //this.Name = cabinet.Name; //this.CabinetNumber.NumberClone(cabinet.CabinetNumber); Cabinet cabinetNum = new Cabinet() { CabinetNumber = new Number { } }; cabinetNum.No = cabinet.No; cabinetNum.Side = cabinet.Side; cabinetNum.Name = cabinet.Name; cabinetNum.CabinetNumber.NumberClone(cabinet.CabinetNumber); Console.WriteLine("CreateCabinetNumber里clone: "); Console.WriteLine("this.GetHashCode: " + cabinetNum.GetHashCode()); Console.WriteLine("this.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode()); Console.WriteLine(); return cabinetNum; } public override string ToString() { if (this.CabinetNumber == null) { return string.Format("{0},{1},{2}", this.No, this.Name, this.Side); } return string.Format("{0},{1},{2},{3}", this.No, this.Name, this.Side, this.CabinetNumber.ToString()); } }
主程序代码:
Console.WriteLine("-----CabinetNumber----"); Cabinet cabinetNum = new Cabinet() { No = 12, Name = "cabinet01", Side = 12, CabinetNumber = new Number() { number = "num01" } }; Cabinet cabinetNumClone = new Cabinet() { CabinetNumber = new Number() }; Console.WriteLine("cabinetNum.GetHashCode: " + cabinetNum.GetHashCode()); Console.WriteLine("cabinetNumClone.GetHashCode: " + cabinetNumClone.GetHashCode()); Console.WriteLine(); Console.WriteLine("cabinetNum.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode()); //Console.WriteLine("cabinetNumClone.CabinetNumber.GetHashCode: " + cabinetNumClone.CabinetNumber.GetHashCode()); Console.WriteLine(); Console.WriteLine("--测试---"); //cabinetNumClone = Cabinet.CreateCabinetNumber(cabinetNum); cabinetNumClone = cabinetNumClone.CreateCabinetNumber(cabinetNum); Console.WriteLine("cabinetNum.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode()); Console.WriteLine("cabinetNumClone.CabinetNumber.GetHashCode: " + cabinetNumClone.CabinetNumber.GetHashCode()); Console.WriteLine(); Console.WriteLine("cabinetNum: " + cabinetNum.ToString()); Console.WriteLine("cabinetNumClone: " + cabinetNumClone.ToString()); Console.WriteLine(); Console.WriteLine("-----改变cabinetNumClone------"); cabinetNumClone.CabinetNumber.number = "num01Clone"; cabinetNumClone.Name = "cabinetClone"; Console.WriteLine("cabinetNum.GetHashCode: " + cabinetNum.GetHashCode()); Console.WriteLine("cabinetNumClone.GetHashCode: " + cabinetNumClone.GetHashCode()); Console.WriteLine(); Console.WriteLine("cabinetNum.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode()); Console.WriteLine("cabinetNumClone.CabinetNumber.GetHashCode: " + cabinetNumClone.CabinetNumber.GetHashCode()); Console.WriteLine(); Console.WriteLine("cabinetNum: " + cabinetNum.ToString()); Console.WriteLine("cabinetNumClone: " + cabinetNumClone.ToString());
标签:cabinetNum,Console,cabinetNumClone,WriteLine,GetHashCode,拷贝,CabinetNumber From: https://www.cnblogs.com/wen-chen/p/16983444.html