对象复制

定义对象

    public class RefPoint
    {      // 定义一个引用类型
        public int x;
        public RefPoint(int x)
        {
            this.x = x;
        }
    }

    public struct ValPoint
    { // 定义一个值类型
        public int x;
        public ValPoint(int x)
        {
            this.x = x;
        }
    }

浅度复制(MemberwiseClone)

    // 将要进行 浅度复制 的对象,注意为 引用类型
    public class RefLine : ICloneable
    {
        public RefPoint rPoint;
        public ValPoint vPoint;
        public RefLine(RefPoint rPoint, ValPoint vPoint)
        {
            this.rPoint = rPoint;
            this.vPoint = vPoint;
        }

        public object Clone()
        {
            return MemberwiseClone();
        }
    }


深度复制

    // 将要进行 浅度复制 的对象,注意为 引用类型
    public class RefLine : ICloneable
    {
        public RefPoint rPoint;
        public ValPoint vPoint;
        public RefLine(RefPoint rPoint, ValPoint vPoint)
        {
            this.rPoint = rPoint;
            this.vPoint = vPoint;
        }

        public object Clone()
        {
            RefPoint rPoint = new RefPoint(this.rPoint.x);       // 对于引用类型,创建新对象
            ValPoint vPoint = this.vPoint;          // 值类型,直接赋值
            RefLine newLine = new RefLine(rPoint, vPoint);
            return newLine;
        }
    }


深度复制(序列化方式)

[Serializable()]
    public class RefPoint
    {      // 定义一个引用类型
        public int x;
        public RefPoint(int x)
        {
            this.x = x;
        }
    }

    [Serializable()]
    public struct ValPoint
    { // 定义一个值类型
        public int x;
        public ValPoint(int x)
        {
            this.x = x;
        }
    }

    // 将要进行 浅度复制 的对象,注意为 引用类型
    [Serializable()]
    public class RefLine : ICloneable
    {
        public RefPoint rPoint;
        public ValPoint vPoint;
        public RefLine(RefPoint rPoint, ValPoint vPoint)
        {
            this.rPoint = rPoint;
            this.vPoint = vPoint;
        }

        public object Clone()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            ms.Position = 0;

            return (bf.Deserialize(ms)); ;
        }
    }


测试代码:

    class Program
    {
        static void Main(string[] args)
        {
            RefPoint rPoint = new RefPoint(1);
            ValPoint vPoint = new ValPoint(1);
            RefLine line = new RefLine(rPoint, vPoint);

            RefLine newLine = (RefLine)line.Clone();
            Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x);
            Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x);

            line.rPoint.x = 10;      // 修改原先的line的 引用类型成员 rPoint
            line.vPoint.x = 10;      // 修改原先的line的 值类型  成员 vPoint
            Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x);
            Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x);

            Console.ReadLine();

        }
    }




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值