欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文章主要是講解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)容介紹會在和大家分享哦

上一篇:C#編程和Visual Studio使用技巧(上)

欄    目:C#教程

下一篇:C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量一

本文標(biāo)題:C#中深度復(fù)制和淺度復(fù)制詳解

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6902.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有