按照书上的代码,深克隆的示例代码编译没通过(可能是印刷时漏掉了某一行代码),所以重新修改了下,贴在这里以供阅读本书时跟我遇到一样问题的园友参考:
浅克隆示例:
要点:克隆之后,新旧对象还是指向同一个引用,不管修改哪一个对象,都会影响另一个对象
namespace
CloneTest
{
class Program
{
static void Main( string [] args)
{
Enrollment sourceStudentsList = new Enrollment();
sourceStudentsList.students.Add( new Student() { Name = " 王小二 " , Age = 27 });
sourceStudentsList.students.Add( new Student() { Name = " 张三 " , Age = 22 });
Enrollment cloneStudentsList = sourceStudentsList.Clone() as Enrollment;
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
cloneStudentsList.students[ 1 ].Name = " 李四 " ;
cloneStudentsList.students[ 1 ].Age = 36 ;
Console.WriteLine( " ---------------------------------------------------------------- " );
Console.WriteLine( " 浅clone之后,修改clone对象将影响source对象 " );
Console.WriteLine( " ---------------------------------------------------------------- " );
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
Console.ReadLine();
}
}
class Student
{
public string Name { set ; get ; }
public Int32 Age { set ; get ; }
public void ShowInfo()
{
Console.WriteLine( " {0}'s age is {1} " , Name, Age);
}
}
class Enrollment : ICloneable
{
public List < Student > students = new List < Student > ();
public void ShowEnrollmentInfo( string Prefix) {
Console.WriteLine(Prefix + " Students enrollment infomation: " );
foreach (Student s in students)
{
s.ShowInfo();
}
}
public object Clone() {
return MemberwiseClone();
}
}
}
{
class Program
{
static void Main( string [] args)
{
Enrollment sourceStudentsList = new Enrollment();
sourceStudentsList.students.Add( new Student() { Name = " 王小二 " , Age = 27 });
sourceStudentsList.students.Add( new Student() { Name = " 张三 " , Age = 22 });
Enrollment cloneStudentsList = sourceStudentsList.Clone() as Enrollment;
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
cloneStudentsList.students[ 1 ].Name = " 李四 " ;
cloneStudentsList.students[ 1 ].Age = 36 ;
Console.WriteLine( " ---------------------------------------------------------------- " );
Console.WriteLine( " 浅clone之后,修改clone对象将影响source对象 " );
Console.WriteLine( " ---------------------------------------------------------------- " );
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
Console.ReadLine();
}
}
class Student
{
public string Name { set ; get ; }
public Int32 Age { set ; get ; }
public void ShowInfo()
{
Console.WriteLine( " {0}'s age is {1} " , Name, Age);
}
}
class Enrollment : ICloneable
{
public List < Student > students = new List < Student > ();
public void ShowEnrollmentInfo( string Prefix) {
Console.WriteLine(Prefix + " Students enrollment infomation: " );
foreach (Student s in students)
{
s.ShowInfo();
}
}
public object Clone() {
return MemberwiseClone();
}
}
}
深克隆示例:
要点:深克隆要求完成克隆后,不管如何设置克隆出的新对象,都不会影响源对象(即新旧对象完全不相干)
using
System;
using System.Collections.Generic;
namespace CloneTest
{
class Program
{
static void Main( string [] args)
{
Enrollment sourceStudentsList = new Enrollment();
sourceStudentsList.students.Add( new Student() { Name = " 王小二 " , Age = 27 });
sourceStudentsList.students.Add( new Student() { Name = " 张三 " , Age = 22 });
Enrollment cloneStudentsList = sourceStudentsList.Clone() as Enrollment;
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
cloneStudentsList.students[ 1 ].Name = " 李四 " ;
cloneStudentsList.students[ 1 ].Age = 36 ;
Console.WriteLine( " ---------------------------------------------------------------- " );
Console.WriteLine( " 深clone之后,修改clone对象不影响source对象 " );
Console.WriteLine( " ---------------------------------------------------------------- " );
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
Console.ReadLine();
}
}
class Student
{
public string Name { set ; get ; }
public Int32 Age { set ; get ; }
public void ShowInfo()
{
Console.WriteLine( " {0}'s age is {1} " , Name, Age);
}
}
class Enrollment : ICloneable
{
public List < Student > students = new List < Student > ();
public void ShowEnrollmentInfo( string Prefix) {
Console.WriteLine(Prefix + " Students enrollment infomation: " );
foreach (Student s in students)
{
s.ShowInfo();
}
}
// 提供一个默认的公有构架函数,以保证Enrollment sourceStudentsList = new Enrollment();能正常编译通过
public Enrollment() { }
/// <summary>
/// 提供了一个私有构造函数
/// </summary>
/// <param name="studentList"></param>
private Enrollment(List < Student > studentList)
{
foreach (Student s in studentList)
{
students.Add( new Student() { Name = s.Name, Age = s.Age }); // 注:原书P309的代码students.Add((Student)s.Clone());编译通不过--提示Student没有Clone方法,所以换成了这个
}
}
public object Clone() {
return new Enrollment(students);
}
}
}
using System.Collections.Generic;
namespace CloneTest
{
class Program
{
static void Main( string [] args)
{
Enrollment sourceStudentsList = new Enrollment();
sourceStudentsList.students.Add( new Student() { Name = " 王小二 " , Age = 27 });
sourceStudentsList.students.Add( new Student() { Name = " 张三 " , Age = 22 });
Enrollment cloneStudentsList = sourceStudentsList.Clone() as Enrollment;
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
cloneStudentsList.students[ 1 ].Name = " 李四 " ;
cloneStudentsList.students[ 1 ].Age = 36 ;
Console.WriteLine( " ---------------------------------------------------------------- " );
Console.WriteLine( " 深clone之后,修改clone对象不影响source对象 " );
Console.WriteLine( " ---------------------------------------------------------------- " );
sourceStudentsList.ShowEnrollmentInfo( " source " );
Console.WriteLine( " ---------------------------------------------------------------- " );
cloneStudentsList.ShowEnrollmentInfo( " clone " );
Console.ReadLine();
}
}
class Student
{
public string Name { set ; get ; }
public Int32 Age { set ; get ; }
public void ShowInfo()
{
Console.WriteLine( " {0}'s age is {1} " , Name, Age);
}
}
class Enrollment : ICloneable
{
public List < Student > students = new List < Student > ();
public void ShowEnrollmentInfo( string Prefix) {
Console.WriteLine(Prefix + " Students enrollment infomation: " );
foreach (Student s in students)
{
s.ShowInfo();
}
}
// 提供一个默认的公有构架函数,以保证Enrollment sourceStudentsList = new Enrollment();能正常编译通过
public Enrollment() { }
/// <summary>
/// 提供了一个私有构造函数
/// </summary>
/// <param name="studentList"></param>
private Enrollment(List < Student > studentList)
{
foreach (Student s in studentList)
{
students.Add( new Student() { Name = s.Name, Age = s.Age }); // 注:原书P309的代码students.Add((Student)s.Clone());编译通不过--提示Student没有Clone方法,所以换成了这个
}
}
public object Clone() {
return new Enrollment(students);
}
}
}