日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)實(shí)例淺析

C#復(fù)制構(gòu)造函數(shù)的一點(diǎn)認(rèn)識:

某些編程語言的類提供了復(fù)制構(gòu)造函數(shù),即從當(dāng)前類實(shí)例構(gòu)造一個新的類實(shí)例。在C#編程語言中,沒有提供復(fù)制構(gòu)造函數(shù),如果要實(shí)現(xiàn)復(fù)制構(gòu)造的功能,就必須自行編寫這樣的構(gòu)造函數(shù)。

實(shí)現(xiàn)C#復(fù)制構(gòu)造函數(shù)的技術(shù)要點(diǎn)

本示例主要說明了如何實(shí)現(xiàn)復(fù)制構(gòu)造函數(shù),技術(shù)要點(diǎn)如下。

◆C#復(fù)制構(gòu)造函數(shù)仍然屬于構(gòu)造函數(shù),不同的是以類本身作為參數(shù)的數(shù)據(jù)類型。

◆調(diào)用構(gòu)造函數(shù)時,參數(shù)必須是已經(jīng)創(chuàng)建的類實(shí)例。

C#復(fù)制構(gòu)造函數(shù)實(shí)現(xiàn)步驟

(1)創(chuàng)建控制臺應(yīng)用程序項(xiàng)目,命名為“CopyConstruct”。

(2)打開并編輯Program.cs文件,代碼如下所示。

 
 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. namespace CopyConstruct 
  5. class Program 
  6. static void Main(string[] args) 
  7. Person personA = new Person();//使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建示例 
  8. personA.FirstName = "Pony"; 
  9. personA.LastName = "Smith"; 
  10. personA.Age = 42; 
  11. Person personB = new Person(personA);//使用C#復(fù)制構(gòu)造函數(shù)創(chuàng)建示例 
  12. Console.WriteLine("personB的數(shù)據(jù):{0} {1}-{2}", 
  13. personB.FirstName, personB.LastName, personB.Age); 
  14. Console.ReadLine(); 
  15. class Person//表示“人”的類 
  16. public string FirstName { get; set; } 
  17. public string LastName { get; set; } 
  18. public int Age { get; set;} 
  19. public Person()//默認(rèn)構(gòu)造函數(shù) 
  20. //C#復(fù)制構(gòu)造函數(shù),以類實(shí)例作為參數(shù)的構(gòu)造函數(shù),將參數(shù)實(shí)例的字段賦給當(dāng)前實(shí)例字段 
  21. public Person(Person source) 
  22. this.FirstName = source.FirstName; 
  23. this.LastName = source.LastName; 
  24. this.Age = source.Age; 

(3)按F5鍵運(yùn)行程序,運(yùn)行結(jié)果如下所示。

personB的數(shù)據(jù):Pony Smith-42

C#復(fù)制構(gòu)造函數(shù)源程序解讀

(1)本示例的Person類中定義了兩個構(gòu)造函數(shù)。一個是默認(rèn)的、不帶參數(shù)的構(gòu)造函數(shù),另一個是以Person類實(shí)例作為參數(shù)的復(fù)制構(gòu)造函數(shù)。在主程序入口Main方法中,首先使用默認(rèn)的構(gòu)造函數(shù)創(chuàng)建Person類的實(shí)例personA,然后給personA中的字段賦值,再將personA類實(shí)例作為Person類復(fù)制構(gòu)造函數(shù)的參數(shù),創(chuàng)建Person類的另一個實(shí)例personB,***將personB的數(shù)據(jù)打印出來。本示例程序的流程圖。

(2)使用構(gòu)造函數(shù)的時候,將在托管內(nèi)存中為新的類實(shí)例分配位置,而不是指向原先的類實(shí)例的托管內(nèi)存位置。在本示例程序中,personB類實(shí)例和personA類實(shí)例不存在引用關(guān)系,其中一個實(shí)例字段的修改不會對另一個實(shí)例產(chǎn)生影響.

C#復(fù)制構(gòu)造函數(shù)的相關(guān)內(nèi)容就向你介紹里,希望對你理解和學(xué)習(xí)C#復(fù)制構(gòu)造函數(shù)有所幫助。


本文題目:C#復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)實(shí)例淺析
鏈接URL:http://www.dlmjj.cn/article/dpicjpi.html