原型模式 對象深淺複製

來源:互聯網
上載者:User

對象的複製:就是對象執行個體化的拷貝,在實際應用中我們其實就用到了對象拷貝,例如實值型別的賦值操作,參考型別的相互賦值,實值型別的裝箱操作等,其實這些都屬於對象的複製操作。不是所有的對象執行個體都可以被複製,它必須實現一個介面:ICloneable介面。該介面的定義:

 

 public interface ICloneable
{
object Clone();
}

 

 

對象的複製可以分兩種:深拷貝、淺拷貝。

淺拷貝:拷貝時實值型別拷貝到新的執行個體,對於參考型別則只拷貝引用。

深拷貝:拷貝對象和來源物件是完全不同的兩個實體,只是這兩個實體的內容一致而已,也就是說不但引用改變了,而且實體也被複製了一份。

 

淺複製(淺拷貝)Demo

 class Program    {        static void Main(string[] args)        {            Student s1 = new Student("Jone", 23);            Student s2 = s1;//此處實際就是一個淺拷貝            s2.Age = 27;            s1.ShowInfo();        }    }    class Student    {        public string Name;        public int Age;        public Student(string name,int age)        {            Name = name;            Age = age;        }        public void ShowInfo()        {            Console.WriteLine("{0}'s age is {1}",Name,Age);            Console.Read();        }    }
實值型別的賦值其實就是一種典型的深拷貝:
public void Main(){     int i=100;     int j=i;     j=200;     Console.WriteLine(i.ToString()); }

//執行結果:100

 在.NET類庫中,System.Array就實現了ICloneable介面,通過反射可以看到其實現的代碼為:

Public object Clone(){      return base.MebmberwiseClone();}

Clone方法通過調用基類System.Object的受保護方法MemberwiseClone實現Array類型的淺拷貝。因為,在.NET中,一切方法都繼承與System.Object 類,所以開發人員可以通過調用MemberClone方法來實現自訂類型的淺拷貝。

 

自訂類型實現拷貝Demo:

實現淺拷貝:

 

代碼

 class Enrollment : ICloneable
{
public List<Student> students = new List<Student>();
public void ShowEnrollmentInfo()
{
Console.WriteLine("以下是所有登記學生的名單:");
foreach (Student item in students)
{
Console.WriteLine("{0}'s age is {1}",item.Name,item.Age);
}
Console.Read();
}

#region ICloneable 成員

public object Clone()
{
return base.MemberwiseClone();//淺拷貝
}

#endregion
}

 

在使用的時候改變拷貝對象的時候會改變來源物件的值,因為淺拷貝用的是同一對象執行個體:

代碼

static void Main(string[] args)
{
Enrollment sourceStudentsList = new Enrollment();
sourceStudentsList.students.Add(new Student("jone", 20));
sourceStudentsList.students.Add(new Student("tom", 21));

//實現對象的複製,執行淺拷貝還是深拷貝
// 取決於Clone方法的實現
Enrollment cloneStudentsList = sourceStudentsList.Clone() as Enrollment;
cloneStudentsList.students[0].Name = "Jonery";

Console.WriteLine(sourceStudentsList.students[0].Name);
Console.Read();
}

 

 

如果要實現深拷貝只要更改Enrollment類:

 

代碼

  class Enrollment : ICloneable
{
public List<Student> students = new List<Student>();
public void ShowEnrollmentInfo()
{
Console.WriteLine("以下是所有登記學生的名單:");
foreach (Student item in students)
{
Console.WriteLine("{0}'s age is {1}",item.Name,item.Age);
}
Console.Read();
}

public Enrollment()
{ }

public Enrollment(List<Student> studentList)
{
foreach (Student item in studentList)
{
students.Add(item);
}
}

#region ICloneable 成員

public object Clone()
{
return new Enrollment(students);//淺拷貝
}

#endregion
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.