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();

        }

    }

  • 相关阅读:
    ASP.NET里面,如果设置了form的 onsubmit="return false;"之后,就不能提交按钮了?
    存储过程(待填)
    关于电脑屏幕分辨率太高字太小怎么解决?
    最新版Android开发工具
    Xamarin For Visual Studio 3.0.54.0 完整离线破解版(C# 开发Android、IOS工具 吾乐吧软件站分享)
    ubuntu 16.04 source (HUST and 163)
    transmission简单使用
    Test Tex
    What is a Statistic?
    IDE、SATA、SCSI、SAS、FC、SSD硬盘类型介绍[zz]
  • 原文地址:https://www.cnblogs.com/chorrysky/p/1498801.html
Copyright © 2011-2022 走看看