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

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

新聞中心

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

C#繼承構(gòu)造函數(shù)存在的意義:大家都知道C#構(gòu)造函數(shù)主要用來設(shè)置類中屬性的初始值,但經(jīng)常會(huì)忽視類的構(gòu)造方法也可以象方法一樣引用調(diào)用父類中的構(gòu)造方法或本身的其他構(gòu)造方法。往往因此寫了很多重復(fù)代碼。下面的代碼介紹了類的構(gòu)造方法的幾種用法。

成都創(chuàng)新互聯(lián)公司秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營(yíng)銷的理念,以專業(yè)定制企業(yè)官網(wǎng),成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,成都小程序開發(fā),網(wǎng)頁設(shè)計(jì)制作,手機(jī)網(wǎng)站制作設(shè)計(jì)全網(wǎng)營(yíng)銷推廣幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長(zhǎng)。

C#繼承構(gòu)造函數(shù)的示例:

 
 
 
  1. using System;   
  2.  
  3. namespace TestApplication   
  4. {   
  5. class Test   
  6. {   
  7. static void Main(string[] args)   
  8. {   
  9.  
  10. TestA testA1 = new TestA();   
  11. Console.WriteLine("測(cè)試類A無參數(shù)構(gòu)造方法");   
  12. Console.WriteLine(testA1.ToString());   
  13. Console.WriteLine();   
  14.  
  15. TestA testA2 = new TestA("Set First Param");   
  16. Console.WriteLine("測(cè)試類A一個(gè)參數(shù)構(gòu)造方法");   
  17. Console.WriteLine(testA2.ToString());   
  18. Console.WriteLine();   
  19.  
  20. TestB testB1= new TestB();   
  21. Console.WriteLine("測(cè)試類B無參數(shù)構(gòu)造方法");   
  22. Console.WriteLine(testB1.ToString());   
  23. Console.WriteLine();   
  24.  
  25. TestB testB2 = new TestB("Set First Param");   
  26. Console.WriteLine("測(cè)試類B一個(gè)參數(shù)構(gòu)造方法");   
  27. Console.WriteLine(testB2.ToString());   
  28. Console.WriteLine();   
  29.  
  30. TestB testB3 = new TestB("Set First Param", "Set Second Param");   
  31. Console.WriteLine("測(cè)試類B兩個(gè)參數(shù)構(gòu)造方法");   
  32. Console.WriteLine(testB3.ToString());   
  33. Console.WriteLine();   
  34.  
  35. TestB testB4 = new TestB("Set First Param",   
  36. "Set Second Param", "Set Third Param");   
  37. Console.WriteLine("測(cè)試類B三個(gè)參數(shù)構(gòu)造方法");   
  38. Console.WriteLine(testB4.ToString());   
  39. Console.WriteLine();   
  40.  
  41. Console.ReadLine();   
  42.  
  43. }   
  44.  
  45. }   
  46. ///    
  47. /// 測(cè)試類A---C#繼承構(gòu)造函數(shù)   
  48. ///    
  49. class TestA   
  50. {   
  51. protected string _testValueA;   
  52.  
  53. ///    
  54. /// 無參數(shù)構(gòu)造方法 --C#繼承構(gòu)造函數(shù)  
  55. ///    
  56. public TestA():this("Set First Param")   
  57. {   
  58.  
  59. }   
  60.  
  61. ///    
  62. /// 一個(gè)參數(shù)構(gòu)造方法 --C#繼承構(gòu)造函數(shù)  
  63. ///    
  64. ///    
  65. public TestA(string value)   
  66. {   
  67. _testValueA = value;   
  68. }   
  69.  
  70. ///    
  71. /// 重新ToString方法   
  72. ///    
  73. ///    
  74. public override string ToString()   
  75. {   
  76. return this._testValueA;   
  77. }   
  78. }   
  79.  
  80. ///    
  81. /// 測(cè)試類TestB,從TestA類中繼承---C#繼承構(gòu)造函數(shù)   
  82. ///    
  83. class TestB : TestA   
  84. {   
  85. protected string _testValueB;   
  86. protected string _testValueC;   
  87. ///    
  88. /// 調(diào)用父類中的構(gòu)造方法   
  89. ///    
  90. public TestB():base()   
  91. {   
  92. this._testValueB = "Set Second Param";   
  93. this._testValueC = "Set Third Param";   
  94. }   
  95. ///    
  96. /// 調(diào)用父類中的構(gòu)造方法--C#繼承構(gòu)造函數(shù)   
  97. ///    
  98. ///    
  99. public TestB(string valueA)   
  100. : base(valueA)   
  101. {   
  102. this._testValueB = "Set Second Param";   
  103. this._testValueC = "Set Third Param";   
  104. }   
  105.  
  106. ///    
  107. /// 調(diào)用其他構(gòu)造方法---C#繼承構(gòu)造函數(shù)   
  108. ///    
  109. ///    
  110. ///    
  111. public TestB(string valueA, string valueB)   
  112. : this(valueA, valueB, "Set Third Param")   
  113. {   
  114.  
  115. }   
  116. ///    
  117. /// 三個(gè)參數(shù)的構(gòu)造方法   
  118. ///    
  119. ///    
  120. ///    
  121. ///    
  122. public TestB(string valueA, string valueB, string valueC)   
  123. {   
  124. this._testValueA = valueA;   
  125. this._testValueB = valueB;   
  126. this._testValueC = valueC;   
  127. }   
  128.  
  129. ///    
  130. /// 重新ToString方法 --C#繼承構(gòu)造函數(shù)  
  131. ///    
  132. ///    
  133. public override string ToString()   
  134. {   
  135. return this._testValueA + "\n" + this._testValueB + "\n" + this._testValueC ;   
  136. }   
  137. }   
  138. }  

C#繼承構(gòu)造函數(shù)示例輸出結(jié)果:

 
 
 
  1. 測(cè)試類A無參數(shù)構(gòu)造方法   
  2. Set First Param   
  3.  
  4. 測(cè)試類A一個(gè)參數(shù)構(gòu)造方法   
  5. Set First Param   
  6.  
  7. 測(cè)試類B無參數(shù)構(gòu)造方法   
  8. Set First Param   
  9.  
  10. Set Second Param   
  11. Set Third Param   
  12.  
  13. 測(cè)試類B一個(gè)參數(shù)構(gòu)造方法   
  14. Set First Param   
  15.  
  16. Set Second Param   
  17. Set Third Param   
  18.  
  19. 測(cè)試類B兩個(gè)參數(shù)構(gòu)造方法   
  20.  
  21. Set First Param   
  22.  
  23. Set Second Param   
  24. Set Third Param   
  25.  
  26. 測(cè)試類B三個(gè)參數(shù)構(gòu)造方法   
  27. Set First Param   
  28.  
  29. Set Second Param   
  30. Set Third Param  

C#繼承構(gòu)造函數(shù)的基本情況就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#繼承構(gòu)造函數(shù)有所幫助。

【編輯推薦】

  1. C#構(gòu)造函數(shù)與C++的區(qū)別淺析
  2. C#構(gòu)造函數(shù)的運(yùn)用淺析
  3. 學(xué)習(xí)C#構(gòu)造函數(shù)的一點(diǎn)體會(huì)
  4. C#靜態(tài)構(gòu)造函數(shù)特點(diǎn)淺析
  5. C#靜態(tài)構(gòu)造函數(shù)學(xué)習(xí)心得淺析

網(wǎng)頁標(biāo)題:C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)淺析
鏈接地址:http://www.dlmjj.cn/article/dhpcdcg.html