定义
不管是深拷贝和浅拷贝 都是两个不同的对象
什么是深拷贝
对像中的引用类型,在堆中是指向了两个不同的地方
什么是浅拷贝
对像中的引用类型,在堆中是指向了两个相同的地方
using System.Runtime.Serialization.Formatters.Binary;
#region 浅拷贝
//Person p1 = new Person();
//p1.Name = "yzk";
//p1.Age = 20;
//p1.Email = "yzk@rupeng.com";
//p1.DaiBu = new Car() { Brand = "奥迪A2" };
========浅拷贝===================
//Person p2 = new Person();
//p2.Name = p1.Name;
//p2.Age = p1.Age;
//p2.Email = p1.Email;
//p2.DaiBu = p1.DaiBu;
Car c1 = p1.DaiBu;
Car c2 = p2.DaiBu;
#endregion
#region 深拷贝
//Person p1 = new Person();
//p1.Name = "yzk";
//p1.Age = 20;
//p1.Email = "yzk@rupeng.com";
//p1.DaiBu = new Car() { Brand = "奥迪A2" };
===========深拷贝=====================
//Person p2 = new Person();
//p2.Name = p1.Name;
//p2.Age = p1.Age;
//p2.Email = p1.Email;
//p2.DaiBu = new Car() { Brand = p1.DaiBu.Brand };
这个根本不叫对象的拷贝
对象的拷贝,无论是深拷贝还是浅拷贝都是又重新创建了一个对象。
//Person p3 = p1;//这个根本没有重新创建对象
//Car c1 = p1.DaiBu;
//Car c2 = p2.DaiBu;
//Console.ReadKey();
#endregion
1.请编程实现一个对象的深拷贝与浅拷贝
//MyClass mc1 = new MyClass();
//mc1.Name = "yzk";
//mc1.Age = 19;
//mc1.MyCar = new Car() { Brand = "捷安特" };
//MyClass mc2 = mc1.QianKaoBei();
//Car c1 = mc1.MyCar;
//Car c2 = mc2.MyCar;
//Console.WriteLine("ok");
//Console.ReadKey();
//===============深拷贝=======================
MyClass mc1 = new MyClass();
mc1.Name = "yzk";
mc1.Age = 19;
mc1.MyCar = new Car() { Brand = "捷安特" };
MyClass mc2 = mc1.ShenKaoBei();
Car c1 = mc1.MyCar;
Car c2 = mc2.MyCar;
Console.WriteLine("ok");
Console.ReadKey();
[Serializable]
public class MyClass
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public Car MyCar
{
get;
set;
}
//实现一个浅拷贝
public MyClass QianKaoBei()
{
//MemberwiseClone()这个方法就是把当前对象"浅拷贝"了一份
return (MyClass)this.MemberwiseClone();
}
//深拷贝
public MyClass ShenKaoBei()
{
//通过二进制序列化实现深拷贝
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, this);
//设置position回到一开始的位置。
ms.Position = 0;
//开始反序列化
MyClass mcObj = (MyClass)bf.Deserialize(ms);
return mcObj;
}
}
}
public class Person
{
//代步工具
public Car DaiBu
{
get;
set;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
}
[Serializable]
public class Car
{
//品牌
public string Brand
{
get;
set;
}
}