亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

淺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 { 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);
        }
    }
}

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消