Ejemplo n.º 1
0
        /// <summary>
        /// 深克隆测试
        /// </summary>
        static void TestICloneableDeep()
        {
            Cup cup = new Cup();

            cup.Height = 20;
            cup.RL     = 200;
            cup.c      = new Colors()
            {
                foot = "白色", Top = "无色"
            };

            Console.WriteLine("***深克隆测试***");
            Console.WriteLine("cup-Height:" + cup.Height); //20
            Console.WriteLine("cup-RL:" + cup.RL);         //200
            Console.WriteLine("cup-foot:" + cup.c.foot);   //白色
            Console.WriteLine("");

            Cup cup1 = (Cup)cup.DeepClone();

            cup1.Height = 10;
            cup1.RL     = 100;
            cup1.c.foot = "灰色";

            Console.WriteLine("cup1-Height:" + cup1.Height); //10
            Console.WriteLine("cup1-RL:" + cup1.RL);         //100
            Console.WriteLine("cup1-foot:" + cup1.c.foot);
            Console.WriteLine("");

            Console.WriteLine("cup-Height:" + cup.Height); //20
            Console.WriteLine("cup-RL:" + cup.RL);         //200
            Console.WriteLine("cup-foot:" + cup.c.foot);   //白色 深克隆后对象中的引用类型的值不发生改变
            Console.WriteLine("");
        }
Ejemplo n.º 2
0
        public object DeepClone()
        {
            Cup cupClone = (Cup)this.MemberwiseClone(); //cupClone是一个浅克隆对象,只克隆了引用地址及值类型值,没有克隆值引用类型值;

            Colors c = new Colors()                     //实例化一个引用类型值新的对象;
            {
                foot = this.c.foot,
                Top  = this.c.Top,
            };

            cupClone.c = c;//通过一个新的实例化对象,传入已经浅克隆的引用类型对象的值,实现深克隆;
            return(cupClone);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 静态扩展浅克隆测试
        /// </summary>
        static void TestCloneExtends()
        {
            Cup cup = new Cup();

            cup.Height = 20;
            cup.RL     = 200;
            cup.c      = new Colors()
            {
                foot = "白色", Top = "无色"
            };

            Console.WriteLine("***静态扩展浅克隆测试***");
            Console.WriteLine("cup-Height:" + cup.Height); //20
            Console.WriteLine("cup-RL:" + cup.RL);         //200
            Console.WriteLine("cup-foot:" + cup.c.foot);   //白色
            Console.WriteLine("");

            Cup cup2 = cup.CloneObject2();
            Cup cup1 = cup.CloneObject();

            cup1.Height = 10;
            cup1.RL     = 100;
            cup1.c.foot = "灰色";



            Console.WriteLine("cup1-Height:" + cup1.Height); //10
            Console.WriteLine("cup1-RL:" + cup1.RL);         //100
            Console.WriteLine("cup1-foot:" + cup1.c.foot);
            Console.WriteLine("");

            Console.WriteLine("cup-Height:" + cup.Height); //20
            Console.WriteLine("cup-RL:" + cup.RL);         //200
            Console.WriteLine("cup-foot:" + cup.c.foot);   //灰色 值发生改变了
            Console.WriteLine("");
            Console.WriteLine("=========================");

            Console.WriteLine("***静态扩展深克隆测试***");
            Console.WriteLine("cup-Height:" + cup2.Height); //20
            Console.WriteLine("cup-RL:" + cup2.RL);         //200
            Console.WriteLine("cup-foot:" + cup2.c.foot);   //白色
            Console.WriteLine("");
            Console.WriteLine("=========================");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 静态扩展深克隆List测试-浅克隆+深克隆
        /// </summary>
        static void TestCloneExtendsList()
        {
            List <Cup> cups           = new List <Cup>();
            List <Cup> cupsClone      = new List <Cup>();
            List <Cup> cupsDeepClone  = new List <Cup>();
            List <Cup> cupsCopyOjbect = new List <Cup>();

            for (int i = 0; i < 3; i++)
            {
                Cup cup = new Cup();
                cup.Height = 1 + i;
                cup.RL     = 10 + i;
                cup.c      = new Colors()
                {
                    foot = "白色", Top = "无色"
                };
                cups.Add(cup);
            }
            cupsClone      = (List <Cup>)cups.CloneList(); //浅克隆
            cupsDeepClone  = cups.DeepCloneObject();       //深克隆
            cupsCopyOjbect = cups.CopyOjbect();

            foreach (var cup in cups) //修改原型数据
            {
                cup.Height = 0;       //修改值类型数据
                cup.c.foot = "红色";    //修改引用类型数据
            }

            Console.WriteLine("***List静态扩展深克隆测试-原型***");
            foreach (var cup in cups)
            {
                Console.WriteLine("cup-Height:" + cup.Height); //20
                Console.WriteLine("cup-RL:" + cup.RL);         //200
                Console.WriteLine("cup-foot:" + cup.c.foot);   //白色
                Console.WriteLine("");
            }
            Console.WriteLine("=================================");
            Console.WriteLine("");

            Console.WriteLine("***List静态扩展深克隆测试-浅克隆***");
            foreach (var cup in cupsClone)
            {
                Console.WriteLine("cup-Height:" + cup.Height); //1、2、3
                Console.WriteLine("cup-RL:" + cup.RL);         //200
                Console.WriteLine("cup-foot:" + cup.c.foot);   //红色
                Console.WriteLine("");
            }
            Console.WriteLine("=================================");
            Console.WriteLine("");

            Console.WriteLine("***List静态扩展深克隆测试-深克隆***");
            foreach (var cup in cupsDeepClone)
            {
                Console.WriteLine("cup-Height:" + cup.Height); //1、2、3
                Console.WriteLine("cup-RL:" + cup.RL);         //200
                Console.WriteLine("cup-foot:" + cup.c.foot);   //白色
                Console.WriteLine("");
            }
            Console.WriteLine("=================================");
            Console.WriteLine("");

            Console.WriteLine("***List静态扩展深克隆测试-深克隆2***");
            foreach (var cup in cupsCopyOjbect)
            {
                Console.WriteLine("cup-Height:" + cup.Height); //1、2、3
                Console.WriteLine("cup-RL:" + cup.RL);         //200
                Console.WriteLine("cup-foot:" + cup.c.foot);   //白色
                Console.WriteLine("");
            }
            Console.WriteLine("=================================");
            Console.WriteLine("");
        }