• 对象复制


    浅度复制(MemberwiseClone)

    演示代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace EventDemo
    {
        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;
            }
        }
    
        // 将要进行 浅度复制 的对象,注意为 引用类型
        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();
            }
        }
    
        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();
    
            }
        }
    }
    View Code

    深度复制

      演示代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace EventDemo
    {
        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;
            }
        }
    
        // 将要进行 浅度复制 的对象,注意为 引用类型
        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;
            }
        }
    
        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();
            }
        }
    }
    View Code

    深度复制(序列化方式)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    
    
    namespace EventDemo
    {
        [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();
            }
        }
    }
    View Code
  • 相关阅读:
    poj2594 Treasure Exploration 二分匹配之最小路径覆盖+传递闭包
    Qsort和Sort排序函数用法
    poj1696 Space Ant 卷包裹法 向量叉积比较
    poj1113 Wall 凸包问题 官方数据
    poj2187 Beauty Contest 凸包求点集的直径
    printf 里面的 %lf 要改成 %f G++,G++的浮点型需要%f输出,%lf就是WA!!!
    让IE6支持Fixed
    更改input type="file" 的样式
    Repeater心得
    Windows服务删除
  • 原文地址:https://www.cnblogs.com/scmail81/p/8678967.html
Copyright © 2020-2023  润新知