浅复制是指当对象的字段值被复制时,字段引用的对象不会被复制・例如,如果一个对象有一个指向字符串的字段,并且我们对该对象做了一个浅复制,那麽两个对象将引用同一个字符串・
实例一:浅复制,字段为值类型;(程序中涉及到的字段类型是值类型,对该字段进行逐位复制)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Resume : ICloneable { private string UserName; private string Sex; private int Age; private string Company; private string TimeArea; public Resume(string Name) { this.UserName = Name; } //设置个人信息 public void SetPerson(string Sex, int Age) { this.Sex = Sex; this.Age = Age; } //设置工作经历 public void SetWorkExperience(string Company, string TimeArea) { this.Company = Company; this.TimeArea = TimeArea; } //显示个人信息 public void Display() { Console.WriteLine("{0} {1} {2}", UserName, Sex, Age); Console.WriteLine("WorkExperience:{1} {0}", Company, TimeArea); } //创建当前对象的浅表复制 //方法是创建一个新的对象,将当前对象的字段信息复制到该新对象 //如果该字段类型为值类型,那么对该字段执行逐位复制 //如果是引用类型,则复制引用但不复制引用对象 public object Clone() { return(Object)this.MemberwiseClone(); } static void Main() { Resume a = new Resume("Big bird"); a.SetPerson("Male", 29); a.SetWorkExperience("1998-2000", "Company A"); Resume b = (Resume)a.Clone(); b.SetWorkExperience("2001-2006", "Company B"); Resume c = (Resume)a.Clone(); c.SetWorkExperience("2007-Now", "Company C"); a.Display(); b.Display(); c.Display(); Console.ReadLine(); } } }
结果:
实例二:浅复制,字段为引用类型;(如果字段涉及到引用类型的话,则复制引用但不复制引用中的对象。)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { //工作经验类 class WorkExperience { private string company; public string Company { set { company = value; } get { return company; } } private string timeArea; public string TimeArea { set { timeArea = value; } get { return timeArea; } } } class Resume : ICloneable { private string UserName; private string Sex; private int Age; private WorkExperience Work; public Resume(string Name) { this.UserName = Name; Work = new WorkExperience(); } //设置个人信息 public void SetPerson(string Sex, int Age) { this.Sex = Sex; this.Age = Age; } //设置工作经历 public void SetWorkExperience(string Company, string TimeArea) { Work.Company = Company; Work.TimeArea = TimeArea; } //显示个人信息 public void Display() { Console.WriteLine("{0} {1} {2}", UserName, Sex, Age); Console.WriteLine("WorkExperience:{1} {0}", Work.Company, Work.TimeArea); } //创建当前对象的浅表复制 //方法是创建一个新的对象,将当前对象的字段信息复制到该新对象 //如果该字段类型为值类型,那么对该字段执行逐位复制 //如果是引用类型,则复制引用但不复制引用对象 public object Clone() { return(Object)this.MemberwiseClone(); } static void Main() { Resume a = new Resume("Big bird"); a.SetPerson("Male", 29); a.SetWorkExperience("1998-2000", "Company A"); Resume b = (Resume)a.Clone(); b.SetWorkExperience("2001-2006", "Company B"); Resume c = (Resume)a.Clone(); c.SetWorkExperience("2007-Now", "Company C"); a.Display(); b.Display(); c.Display(); Console.ReadLine(); } } }
结果:
深复制是将指向内容复制到给当前对象新分配的缓冲区中的一种复制方式,相对于浅复制。
是对对象事例中字段引用的对象也进行复制的一种方式.所以,如果一个对象有一个指向字符串的字段,并且对该对象做了一个深复制的话.我们将创建一个新的对象和一个新的字符串.-新对象将指向新字符串.
实例三:深复制,字段为引用类型;(深复制是把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { //工作经验类 class WorkExperience:ICloneable { private string company; public string Company { set { company = value; } get { return company; } } private string timeArea; public string TimeArea { set { timeArea = value; } get { return timeArea; } } //创建当前对象的浅表复制 //方法是创建一个新的对象,将当前对象的字段信息复制到该新对象 //如果该字段类型为值类型,那么对该字段执行逐位复制 //如果是引用类型,则复制引用但不复制引用对象 public object Clone() { return (Object)this.MemberwiseClone(); } } class Resume:ICloneable { private string UserName; private string Sex; private int Age; private WorkExperience Work; public Resume(string Name) { this.UserName = Name; Work = new WorkExperience(); } /// 克隆工作经验数据 private Resume(WorkExperience Work) { this.Work = (WorkExperience)Work.Clone(); } //设置个人信息 public void SetPerson(string Sex,int Age) { this.Sex = Sex; this.Age = Age; } //设置工作经历 public void SetWorkExperience(string Company, string TimeArea) { Work.Company = Company; Work.TimeArea = TimeArea; } //显示个人信息 public void Display() { Console.WriteLine("{0} {1} {2}", UserName, Sex, Age); Console.WriteLine("WorkExperience:{1} {0}", Work.Company, Work.TimeArea); } /// 调用私有构造函数克隆工作经验 /// 将相关字段赋给当前对象 /// 实现深复制克隆的完成 public Object Clone() { Resume obj = new Resume(this.Work); obj.UserName = this.UserName; obj.Sex = this.Sex; return obj; } static void Main() { Resume a = new Resume("Big bird"); a.SetPerson("Male", 29); a.SetWorkExperience("1998-2000", "Company A"); Resume b = (Resume)a.Clone(); b.SetWorkExperience("2001-2006", "Company B"); Resume c = (Resume)a.Clone(); c.SetWorkExperience("2007-Now", "Company C"); a.Display(); b.Display(); c.Display(); Console.ReadLine(); } } }
结果:
参考: http://blog.csdn.net/cjr15233661143/article/details/8583102