简述C#中浅复制和深复制

引用类型直接进行“=”来复制是不可以的,简单的复制成员可以通过派生在System.Object中的MemberwiseClone()方法来完成。这是一个受保护的方法。

我们简单的代码实现如下:

    public abstract class Animal
    {
        public string Name { get; set; }
        public double Weight { get; set; }
        public abstract void Sounds();
    }
    public class Dog : Animal, ICloneable
    {
        public override void Sounds()
        {
            Console.WriteLine("{0}汪汪汪", Name);
        }
        public object Clone()
        {
            return (Object)this.MemberwiseClone(); //实现克隆方法
        }
        public object Clone(string name, double weight)
        {
            var dog = (Dog)this.Clone();
            dog.Name = name;
            dog.Weight = weight;
            return dog;
        }
    }

Dog类实现了接口ICloneable。在main函数中编写测试代码

        static void Main(string[] args)
        {
            Dog dog1 = new Dog { Name = "二黄", Weight = 20.0 };
            dog1.Sounds();
            var dog2 = (Dog)dog1.Clone("二哈", 19.0);
            dog2.Sounds();
            Console.ReadKey();
        }

运行结果如下


我们修改一下Dog类,添加主人Master

    public class Master
    {
        public string Name { get; set; }
    }
    public class Dog : Animal, ICloneable
    {
        private Master master;
        public Dog()
        {
            master = new Master();
        }
        public override void Sounds()
        {
            Console.WriteLine("{0}家的狗:{1}正在汪汪汪叫", master.Name, Name);
        }
        public void SetMasterName(string name)
        {
            master.Name = name;
        }
        public object Clone()
        {
            return (Object)this.MemberwiseClone(); //实现克隆方法
        }
        public object Clone(string name, double weight)
        {
            var dog = (Dog)this.Clone();
            dog.Name = name;
            dog.Weight = weight;
            return dog;
        }
    }

这里引用了外部的对象

        static void Main(string[] args)
        {
            Dog dog1 = new Dog { Name = "二黄", Weight = 20.0 };
            dog1.SetMasterName("张三");
            var dog2 = (Dog)dog1.Clone("二哈", 19.0);
            dog2.SetMasterName("李四");
            dog1.Sounds();
            dog2.Sounds();
            Console.ReadKey();
        }

结果如下:


这不是我们想看到的,我们需要修改引用Master类也实现ICloneable接口。

    public class Master: ICloneable
    {
        public string Name { get; set; }
        public object Clone()
        {
            return (Object)this.MemberwiseClone(); //实现克隆方法
        }
    }
    public class Dog : Animal, ICloneable
    {
        private Master master;
        public Dog()
        {
            master = new Master();
        }
        public override void Sounds()
        {
            Console.WriteLine("{0}家的狗:{1}正在汪汪汪叫", master.Name, Name);
        }
        public void SetMasterName(string name)
        {
            master.Name = name;
        }
        public object Clone()
        {
            return (Object)this.MemberwiseClone(); //实现克隆方法
        }
        public object Clone(string name, double weight)
        {
            var dog = (Dog)this.Clone();
            dog.Name = name;
            dog.Weight = weight;
            dog.master= (Master)master.Clone();
            return dog;
        }
    }

最后得到我们想要的结果:


posted @ 2018-03-27 01:36  huey-chan  阅读(202)  评论(0编辑  收藏  举报