最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年10月13日 11:47:33   投稿:lijiao  
這篇文章主要介紹了C#中深度復(fù)制和淺度復(fù)制的相關(guān)資料,需要的朋友可以參考下

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

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

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

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

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()進(jìn)行淺度復(fù)制,使用getCopy方法.
    public object getCopy()
    {
      return MemberwiseClone();  
    }
 
    //深度復(fù)制:
    public object clone()
    {
      Cloner clonedCloner = new Cloner(MyContent.val); //此處是實(shí)例化一個(gè)對(duì)象
      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);
    }
  }
}

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

相關(guān)文章

最新評(píng)論

郯城县| 荃湾区| 保亭| 辽宁省| 保德县| 贵南县| 广河县| 张北县| 获嘉县| 余江县| 水城县| 益阳市| 永丰县| 依安县| 鸡泽县| 云林县| 泸定县| 老河口市| 云林县| 凤台县| 河东区| 贵港市| 拉孜县| 台安县| 澄迈县| 朝阳县| 黄龙县| 勃利县| 成武县| 丹凤县| 乌兰浩特市| 龙山县| 娄烦县| 锡林浩特市| 曲阜市| 镇原县| 康平县| 阳原县| 佛山市| 浙江省| 肇庆市|