自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)及調(diào)用淺析

開(kāi)發(fā) 后端
C#繼承構(gòu)造函數(shù)的實(shí)現(xiàn)和調(diào)用經(jīng)常是面試時(shí)會(huì)碰到的問(wèn)題,那么本文就向你介紹C#繼承構(gòu)造函數(shù)的具體實(shí)現(xiàn)和調(diào)用情況。

C#類的繼承,構(gòu)造函數(shù)實(shí)現(xiàn)及其調(diào)用順序

類層層派生,在實(shí)例化的時(shí)候構(gòu)造函數(shù)的調(diào)用順序是怎樣的? --從頂層基類開(kāi)始向子類方向順序調(diào)用無(wú)參構(gòu)造.

默認(rèn)構(gòu)造(無(wú)參構(gòu)造)和帶參構(gòu)造什么時(shí)候調(diào)用?--默認(rèn)將從頂層父類的默認(rèn)構(gòu)造一直調(diào)用到當(dāng)前類的默認(rèn)構(gòu)造.

下面是C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)及調(diào)用示例:

  1. /**//*--===------------------------------------------===---  
  2. 作者:許明會(huì)  
  3. 日期:類的派生和構(gòu)造函數(shù)間的關(guān)系,調(diào)用層次及實(shí)現(xiàn)  
  4. 日期:2009年7月18日 17:30:43  
  5. 若希望類能夠有派生類,必須為其實(shí)現(xiàn)默認(rèn)構(gòu)造函數(shù).  
  6. 若類沒(méi)有實(shí)現(xiàn)帶參構(gòu)造,編譯器將自動(dòng)創(chuàng)建默認(rèn)構(gòu)造函數(shù).  
  7. 若類實(shí)現(xiàn)了帶參構(gòu)造,則編譯器不會(huì)自動(dòng)生成默認(rèn)構(gòu)造.  
  8. --===------------------------------------------===---*/ 
  9. using System;  //C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)及調(diào)用
  10.  
  11. namespace xumh  
  12. {  
  13. public class MyClass  
  14. {  
  15. public MyClass()  
  16. {  
  17. Console.WriteLine("MyClass:默認(rèn)構(gòu)造函數(shù)");  
  18. }  
  19. public MyClass(int a, int b)  
  20. {  
  21. Console.WriteLine("MyClass帶參構(gòu)造:a={0}, b={1}.", a, b);  
  22. }  
  23. }  
  24.  
  25. public class MyClass2 : MyClass  
  26. {  
  27. public MyClass2()  
  28. {  
  29. Console.WriteLine("MyClass2:默認(rèn)構(gòu)造函數(shù)");  
  30. }  
  31. public MyClass2(int a, int b)  
  32. {  
  33. Console.WriteLine("MyClass2帶參構(gòu)造:a={0}, b={1}.", a, b);  
  34. }  
  35. }  
  36.  //C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)及調(diào)用
  37. public class MyClass3 : MyClass2  
  38. {  
  39.  
  40. public MyClass3()  
  41. {  
  42. Console.WriteLine("MyClass3:默認(rèn)構(gòu)造函數(shù)");  
  43. }  
  44. public MyClass3(int a, int b)  
  45. {  
  46. Console.WriteLine("MyClass3帶參構(gòu)造:a={0}, b={1}.", a, b);  
  47. }  
  48. }  
  49.  
  50. public class runMyApp  
  51. {  
  52. static void Main()  
  53. {  
  54. MyClass3 my = new MyClass3(3,4);  
  55. }  
  56. }  
  57. }  //C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)及調(diào)用
  58. /**//*--===------------------------------------------===---  
  59. 輸出如下:  
  60. MyClass:默認(rèn)構(gòu)造函數(shù)  
  61. MyClass2:默認(rèn)構(gòu)造函數(shù)  
  62. MyClass3帶參構(gòu)造:a=3, b=4.  
  63. --===------------------------------------------===---*/ 

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

【編輯推薦】

  1. 學(xué)習(xí)C#構(gòu)造函數(shù)的一點(diǎn)體會(huì)
  2. C#靜態(tài)構(gòu)造函數(shù)特點(diǎn)淺析
  3. C#靜態(tài)構(gòu)造函數(shù)學(xué)習(xí)心得淺析
  4. C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)淺析
  5. C#繼承與構(gòu)造函數(shù)的調(diào)用實(shí)例演示
責(zé)任編輯:仲衡 來(lái)源: 百度空間
相關(guān)推薦

2009-08-13 18:15:06

C#繼承構(gòu)造函數(shù)

2009-08-14 09:15:28

C#調(diào)用構(gòu)造函數(shù)

2009-08-13 13:42:54

C#構(gòu)造函數(shù)

2009-07-31 15:44:02

C#靜態(tài)構(gòu)造函數(shù)

2009-08-13 18:26:35

C#繼承構(gòu)造函數(shù)

2009-08-13 17:38:42

C#構(gòu)造函數(shù)

2009-08-13 18:02:11

C#靜態(tài)構(gòu)造函數(shù)

2009-09-18 09:02:45

CLR Via C#

2009-08-14 09:50:46

C#復(fù)制構(gòu)造函數(shù)

2009-08-14 09:43:59

C#復(fù)制構(gòu)造函數(shù)

2009-08-14 09:58:09

C#復(fù)制構(gòu)造函數(shù)

2009-08-13 17:30:30

C#構(gòu)造函數(shù)

2009-09-07 05:24:22

C#窗體繼承

2009-09-04 10:05:16

C#調(diào)用瀏覽器瀏覽器的原理

2009-08-13 18:10:31

C#靜態(tài)構(gòu)造函數(shù)

2009-08-21 11:24:16

C#異步調(diào)用

2009-08-04 09:30:33

C#調(diào)用ImageAn

2009-08-14 09:27:27

C#構(gòu)造函數(shù)的特性

2009-07-31 16:25:29

C#回調(diào)函數(shù)API應(yīng)用

2009-07-31 14:15:38

C# 構(gòu)造函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)