zoukankan      html  css  js  c++  java
  • 通过序列化实现深拷贝

    通过序列化实现深拷贝

    要实现深拷贝,可以通过序列化的方式。抽象类及具体类都必须标注为可序列化的[Serializable],上面的例子加上深拷贝之后的完整程序如下:

    using System;

    using System.Collections;

    using System.IO;

    using System.Runtime.Serialization;

    using System.Runtime.Serialization.Formatters.Binary;

    [Serializable]

    abstract class ColorPrototype

    {

        public abstract ColorPrototype Clone(bool Deep);

    }

    [Serializable]

    class ConcteteColorPrototype : ColorPrototype

    {

        private int _red, _green, _blue;

        public ConcteteColorPrototype(int red, int green, int blue)

        {

            this._red = red;

            this._green = green;

            this._blue = blue;

        }

         public override ColorPrototype Clone(bool Deep)

        {

            if(Deep)

                return CreateDeepCopy();

            else

                return (ColorPrototype) this.MemberwiseClone();

        }

        //实现深拷贝

        public ColorPrototype CreateDeepCopy()

        {

            ColorPrototype colorPrototype;

            MemoryStream memoryStream = new MemoryStream();

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(memoryStream, this);

            memoryStream.Position = 0;

            colorPrototype = (ColorPrototype)formatter.Deserialize(memoryStream);

            return colorPrototype;

        }

        public ConcteteColorPrototype Create(int red,int green,int blue)

        {

            return new ConcteteColorPrototype(red,green,blue); 

        }

        public void Display(string _colorname)

        {

            Console.WriteLine("{0}'s RGB Values are: {1},{2},{3}",

                _colorname,_red, _green, _blue );

        }

    }

    class ColorManager

    { 

        Hashtable colors = new Hashtable();

         public ColorPrototype this[string name]

        {

            get

            {

                return (ColorPrototype)colors[name];

            }

            set

            {

                colors.Add(name,value);

            }

        }

    }

    class App

    {

        public static void Main(string[] args)

        {

            ColorManager colormanager = new ColorManager();

            //初始化颜色

            colormanager["red"] = new ConcteteColorPrototype(255, 0, 0);

            colormanager["green"] = new ConcteteColorPrototype(0, 255, 0);

            colormanager["blue"] = new ConcteteColorPrototype(0, 0, 255);

            colormanager["angry"] = new ConcteteColorPrototype(255, 54, 0);

            colormanager["peace"] = new ConcteteColorPrototype(128, 211, 128);

            colormanager["flame"] = new ConcteteColorPrototype(211, 34, 20);

            //使用颜色

            string colorName = "red";

            ConcteteColorPrototype c1 = (ConcteteColorPrototype)colormanager[colorName].Clone(false);

            c1.Display(colorName);

            colorName = "peace";

            ConcteteColorPrototype c2 = (ConcteteColorPrototype)colormanager[colorName].Clone(true);

            c2.Display(colorName);

            colorName = "flame";

            ConcteteColorPrototype c3 = (ConcteteColorPrototype)colormanager[colorName].Clone(true);

            c3.Display(colorName);

            Console.ReadLine();

        }

    }

  • 相关阅读:
    pdf在线转换器
    抖音修复老照片动起来笑起来的程序app的下载地址
    FFmpeg.AutoGen Unable to load DLL 'avutil.56' 解决方法
    Array.prototype.fill 填充值被复用的问题
    Recoil Input 光标位置被重置到末尾的问题
    TypeScript 扩展全局 Window 时报错的解决
    Recoil 中默认值的正确处理
    Recoil 中多级数据联动及数据重置的合理做法
    Recoil 默认值及数据级联的使用
    Recoil 的使用
  • 原文地址:https://www.cnblogs.com/chorrysky/p/1498801.html
Copyright © 2011-2022 走看看