国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中深度復(fù)制和淺度復(fù)制詳解

2020-01-24 01:25:36
字體:
供稿:網(wǎng)友

本文章主要是講解C# 語言編程中,深度復(fù)制和淺度復(fù)制,下面我將通過一個實例進行講解。在實例開發(fā)之前,我們得先知道深度復(fù)制是什么和淺度復(fù)制是什么,它們之間的區(qū)別又是什么,下面將為大家一一揭曉。

1.深度復(fù)制是什么?
深度復(fù)制就是引用類型的復(fù)制。

2.淺度復(fù)制是什么?
淺度復(fù)制是值類型的復(fù)制。

以下是C#中深度復(fù)制和淺度復(fù)制的實例代碼引用片段:

using System;using System.Collections.Generic;using System.Text;namespace Test{  public class Content  {    public int val;  }   //此處若是深度復(fù)制才繼承ICloneable接口  //public class Cloner : ICloneable  public class Cloner  {    public Content MyContent = new Content();    public Cloner(int newVal)    {      MyContent.val = newVal;    }     //淺度復(fù)制    //使用System.Object.MemberwiseClone()進行淺度復(fù)制,使用getCopy方法.    public object getCopy()    {      return MemberwiseClone();      }     //深度復(fù)制:    public object clone()    {      Cloner clonedCloner = new Cloner(MyContent.val); //此處是實例化一個對象      return clonedCloner;    }  }} //主函數(shù)using System;using System.Collections.Generic;using System.Text;namespace Test{  class Program  {    static void Main(string[] args)    {      Cloner mySource = new Cloner(5);      Cloner myTarget = (Cloner)mySource.getCopy();//深度為cloner      Console.WriteLine("MyTarget.Mycontent.Val={}",myTarget.MyContent.val);      mySource.MyContent.val = 2;      Console.WriteLine("MyTarget.Mycontent.Val={}", myTarget.MyContent.val);    }  }}

通過簡單的實例開發(fā),大家對深度復(fù)制和淺度復(fù)制是不是有了大概的了解了,以后再有相關(guān)的內(nèi)容介紹會在和大家分享哦

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平山县| 康乐县| 崇文区| 博客| 亳州市| 浠水县| 新野县| 凤阳县| 大姚县| 江孜县| 康马县| 临海市| 宜兰市| 贵德县| 个旧市| 黄平县| 吉林市| 阿城市| 江北区| 鸡泽县| 桐乡市| 孝义市| 从化市| 济阳县| 北碚区| 什邡市| 金沙县| 彭州市| 怀远县| 长兴县| 邢台县| 汝城县| 吉安县| 苗栗县| 安阳市| 鄂伦春自治旗| 安国市| 抚松县| 溧水县| 宁陕县| 十堰市|