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


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

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

    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 { setget; }
            
    public Int32 Age { setget; }
           

            
    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 { setget; }
            
    public Int32 Age { setget; }
           

            
    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);
            }
        }
    }
    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    yii中sphinx索引配置解析
    Java HashMap工作原理及实现?
    Spring AOP 实现原理与 CGLIB 应用
    Spring 容器AOP的实现原理——动态代理
    代理模式(静态代理)
    Java中的字符串常量池
    JAVA对象的深度克隆
    重载(Overload)和重写(Override)的区别。重载的方法能否根据返回类型进行区分?
    访问修饰符public,private,protected和default的区别?
    Java中的字符串常量池?
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/1454013.html
Copyright © 2020-2023  润新知