public void Execute()
        {
            //浅克隆:创建新的对象,指向老对象的值
            //深克隆: 创建新的对象,指向新创建的老对象的值
            Console.WriteLine("请选择克隆方式:1.浅克隆 2.深克隆");
            string cloneType = Console.ReadLine();

            AbstractPrototype prototype,copy;
            prototype = new ConcretePrototype();

            if (cloneType=="1")
                copy=prototype.ShallowClone();
            else
                copy = prototype.DeepClone();
            Console.WriteLine("对象是否相同" + (prototype == copy));
            Console.WriteLine("值是否相同" + (prototype.entity == copy.entity));
        }
Beispiel #2
0
        static private void DeepCloneTest()
        {
            //创建原型 a, 并浅拷贝为 b。
            ConcretePrototype a = new ConcretePrototype();
            ConcretePrototype b = (ConcretePrototype)a.DeepClone();

            Debug.Log(a.id + ", " + a.name + ", " + a.extra.id + ", " + a.extra.name);   //1, A, 1, A
            Debug.Log(b.id + ", " + b.name + ", " + b.extra.id + ", " + b.extra.name);   //1, A, 1, A

            //尝试修改b
            b.id         = 2;
            b.name       = "B";
            b.extra.id   = 2;
            b.extra.name = "B";

            //B正常,全部被修改。
            //A正常,没有因为B的修改而被修改。
            Debug.Log(a.id + ", " + a.name + ", " + a.extra.id + ", " + a.extra.name);   //1, A, 1, A
            Debug.Log(b.id + ", " + b.name + ", " + b.extra.id + ", " + b.extra.name);   //2, B, 2, B
        }
Beispiel #3
0
        public void Execute()
        {
            //浅克隆:创建新的对象,指向老对象的值
            //深克隆: 创建新的对象,指向新创建的老对象的值
            Console.WriteLine("请选择克隆方式:1.浅克隆 2.深克隆");
            string cloneType = Console.ReadLine();

            AbstractPrototype prototype, copy;

            prototype = new ConcretePrototype();

            if (cloneType == "1")
            {
                copy = prototype.ShallowClone();
            }
            else
            {
                copy = prototype.DeepClone();
            }
            Console.WriteLine("对象是否相同" + (prototype == copy));
            Console.WriteLine("值是否相同" + (prototype.entity == copy.entity));
        }