日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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

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

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("測試類A無參數(shù)構(gòu)造方法");   
  12. Console.WriteLine(testA1.ToString());   
  13. Console.WriteLine();   
  14.  
  15. TestA testA2 = new TestA("Set First Param");   
  16. Console.WriteLine("測試類A一個參數(shù)構(gòu)造方法");   
  17. Console.WriteLine(testA2.ToString());   
  18. Console.WriteLine();   
  19.  
  20. TestB testB1= new TestB();   
  21. Console.WriteLine("測試類B無參數(shù)構(gòu)造方法");   
  22. Console.WriteLine(testB1.ToString());   
  23. Console.WriteLine();   
  24.  
  25. TestB testB2 = new TestB("Set First Param");   
  26. Console.WriteLine("測試類B一個參數(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("測試類B兩個參數(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("測試類B三個參數(shù)構(gòu)造方法");   
  38. Console.WriteLine(testB4.ToString());   
  39. Console.WriteLine();   
  40.  
  41. Console.ReadLine();   
  42.  
  43. }   
  44.  
  45. }   
  46. ///    
  47. /// 測試類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. /// 一個參數(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. /// 測試類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. /// 三個參數(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. 測試類A無參數(shù)構(gòu)造方法   
  2. Set First Param   
  3.  
  4. 測試類A一個參數(shù)構(gòu)造方法   
  5. Set First Param   
  6.  
  7. 測試類B無參數(shù)構(gòu)造方法   
  8. Set First Param   
  9.  
  10. Set Second Param   
  11. Set Third Param   
  12.  
  13. 測試類B一個參數(shù)構(gòu)造方法   
  14. Set First Param   
  15.  
  16. Set Second Param   
  17. Set Third Param   
  18.  
  19. 測試類B兩個參數(shù)構(gòu)造方法   
  20.  
  21. Set First Param   
  22.  
  23. Set Second Param   
  24. Set Third Param   
  25.  
  26. 測試類B三個參數(shù)構(gòu)造方法   
  27. Set First Param   
  28.  
  29. Set Second Param   
  30. Set Third Param  

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

【編輯推薦】

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

分享名稱:C#繼承構(gòu)造函數(shù)實現(xiàn)淺析
網(wǎng)站URL:http://m.5511xx.com/article/dhpcdcg.html