首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[你必须知道的.Net]读书笔记--浅clone与深clone

[你必须知道的.Net]读书笔记--浅clone与深clone

作者头像
菩提树下的杨过
发布2018-01-23 10:55:05
8860
发布2018-01-23 10:55:05
举报

按照书上的代码,深克隆的示例代码编译没通过(可能是印刷时漏掉了某一行代码),所以重新修改了下,贴在这里以供阅读本书时跟我遇到一样问题的园友参考:

浅克隆示例: 要点:克隆之后,新旧对象还是指向同一个引用,不管修改哪一个对象,都会影响另一个对象

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();
        }
    }
}

深克隆示例:

要点:深克隆要求完成克隆后,不管如何设置克隆出的新对象,都不会影响源对象(即新旧对象完全不相干)

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);
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-05-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档