欢迎使用CSDN-markdown编辑器

namespace Light_Sharp

{

    public class Printor

    {

        int printID;

        public int PrintID

        {

            get;

            set;

        }

        public Printor(int newID)

        {

            PrintID = newID;a

        }

        public object GetCopy()

        {

            return MemberwiseClone();

        }        

    }

    public class program

    {

        public static void Main(string[] args)

        {

            Printor print1 = new Printor(13000);

            Printor copytprint = (Printor)print1.GetCopy();

            print1.PrintID = 12000;

            Console.WriteLine("print1 ID:{0}",print1.PrintID);

            Console.WriteLine("copyprint ID:{0}",copytprint.PrintID);

            Console.ReadKey();

        }

    }



}

运行程序会得到输出:
print1 ID:12000

copyprint ID:13000

浅复制适用于用于类成员变量没有引用的情况下,如果有引用变量,就要用深复制了。

下面的代码反应了如果成员变量有引用会发生的状况,当我们修改对象print1中carbon的硬度hard时,copyprint中carbon的硬度hard也变成了9,那是因为两个对象中的carbon引用实际上指向

了同一个对象。

输出结果:

print1’s carbon hard:9

copyprint’s carbon hard:9

namespace Light_Sharp

{

    public class Carbon

    {

        int hard;

        public int Hard

        {

            get;

            set;

        }

        public Carbon(int hard)

        {

            Hard = hard;

        }

    }

    public class Printor

    {

        public Carbon carbon = new Carbon(10);

        int printID;

        public int PrintID

        {

            get;

            set;

        }

        public Printor(int newID)

        {

            PrintID = newID;

        }

        public object GetCopy()

        {

            return MemberwiseClone();

        }    

    }



    public class program

    {

        public static void Main(string[] args)

        {

            Printor print1 = new Printor(13000);

            Printor copytprint = (Printor)print1.GetCopy();

            print1.carbon.Hard = 9;

            Console.WriteLine("print1's carbon hard:{0}",print1.carbon.Hard);

            Console.WriteLine("copyprint's carbon hard:{0}", copytprint.carbon.Hard);

            Console.ReadKey();

        }

    } 

}

为了实现深复制,那么该怎么办呢?

其实微软的.NetFramework提供了标准的实现方式——继承ICloneable接口,

该接口有一个Clone()方法,该方法不带任何参数,并且返回一个object类型的对象。

签名跟上面的GetCopy()方法相同。修改Printor类,已实现深复制代码如下:

这里写代码片public class Carbon

    {

        int hard;

        public int Hard

        {

            get;

            set;

        }

        public Carbon(int hard)

        {

            Hard = hard;

        }

    }

    public class Printor : ICloneable

    {

        public Carbon carbon = new Carbon(10);

        int printID;

        public int PrintID

        {

            get;

            set;

        }

        public Printor(int newID)

        {

            PrintID = newID;

        }



        public object Clone()

        {

            Printor printorClone = new Printor(PrintID);

            printorClone.carbon.Hard = carbon.Hard;

            return printorClone;

        }     

    }

    public class program

    {

        public static void Main(string[] args)

        {

            Printor print1 = new Printor(13000);

            Printor copytprint = (Printor)print1.Clone();

            print1.carbon.Hard = 9;

            Console.WriteLine("print1's carbon hard:{0}",print1.carbon.Hard);

            Console.WriteLine("copyprint's carbon hard:{0}", copytprint.carbon.Hard);

            Console.ReadKey();

        }

    }

输出结果:

print1’s carbon hard:9

copyprint’s carbon hard:10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值