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

C#繼承與構(gòu)造函數(shù)的調(diào)用實(shí)例演示

開發(fā) 后端
C#繼承構(gòu)造函數(shù)的調(diào)用實(shí)例演示向你介紹了在實(shí)際操作過程中C#繼承構(gòu)造函數(shù)的調(diào)用順序以及步驟。

C#繼承構(gòu)造函數(shù)的調(diào)用實(shí)例演示

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace test  
  7. {  
  8. class Program  
  9. {  
  10. static void Main(string[] args)  
  11. {  
  12. //***種情況  --C#繼承構(gòu)造函數(shù)的調(diào)用
  13. A a = new B();//x=1,y=0  
  14. a.PrintFields();//x=1,y=-1  
  15. //因?yàn)闃?gòu)造B之前,先執(zhí)行變量,y沒有明確賦值,默認(rèn)為0。  
  16. //A構(gòu)造函數(shù)調(diào)用的PrintFields方法在A類里是虛函數(shù),它的實(shí)現(xiàn)是在B類,  
  17. //所以執(zhí)行B類的PrintFields方法,結(jié)果輸出。  
  18. //雖然繼續(xù)執(zhí)行完B的構(gòu)造函數(shù),使y的值是-1.但結(jié)果之前已經(jīng)輸出  
  19.  
  20. //第二種情況  --C#繼承構(gòu)造函數(shù)的調(diào)用
  21. B b = new B();//x=1,y=0  
  22. b.PrintFields();//x=1,y=-1  
  23. //因?yàn)闃?gòu)造B之前,先執(zhí)行變量,y沒有明確賦值,默認(rèn)為0。  
  24. //執(zhí)行B的構(gòu)造函數(shù),因?yàn)锽繼承A,所以先執(zhí)行A的構(gòu)造函數(shù)。//  
  25. A構(gòu)造函數(shù)調(diào)用的PrintFields方法在A類里是虛函數(shù),它的實(shí)現(xiàn)是在B類,  
  26. //所以執(zhí)行B類的PrintFields方法,結(jié)果輸出。  
  27. //雖然繼續(xù)執(zhí)行完B的構(gòu)造函數(shù),使y的值是-1.但結(jié)果之前已經(jīng)輸出 //第三種情況  
  28. A c = new A();  
  29. c.PrintFields();//什么都不輸出  
  30.  
  31. Console.ReadKey();  
  32. }  
  33.  
  34. }  
  35.  
  36. class A  //C#繼承構(gòu)造函數(shù)的調(diào)用
  37. {  
  38. public A()  
  39. {  
  40. PrintFields();  
  41.  
  42. }  
  43. public virtual void PrintFields()  
  44. { }  
  45. }  
  46. class B : A  
  47. {  
  48. int x = 1;  
  49. int y;  
  50. public B()  
  51. {  
  52. y = -1;  
  53. }  
  54. public override void PrintFields()  
  55. {  
  56. Console.WriteLine("x={0},y={1}", x, y);  
  57. }  
  58. }  
  59.  

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

【編輯推薦】

  1. C#構(gòu)造函數(shù)的運(yùn)用淺析
  2. 學(xué)習(xí)C#構(gòu)造函數(shù)的一點(diǎn)體會(huì)
  3. C#靜態(tài)構(gòu)造函數(shù)特點(diǎn)淺析
  4. C#靜態(tài)構(gòu)造函數(shù)學(xué)習(xí)心得淺析
  5. C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)淺析
責(zé)任編輯:仲衡 來源: 中國自學(xué)編程網(wǎng)
相關(guān)推薦

2009-09-01 18:29:10

C#繼承C#多態(tài)

2009-08-13 18:36:36

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

2009-10-23 11:31:05

CLR Via C#調(diào)

2009-08-13 18:15:06

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

2009-08-26 15:35:59

C#虛函數(shù)

2009-08-14 09:15:28

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

2024-12-31 00:07:12

2009-08-18 13:30:01

C#安裝與部署

2009-08-13 14:36:40

C#結(jié)構(gòu)體構(gòu)造函數(shù)

2009-08-14 09:50:46

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

2009-08-13 17:30:30

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

2009-08-18 10:17:25

C#枚舉類型

2009-07-31 14:15:38

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

2009-08-24 18:09:13

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

2009-07-30 15:24:13

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

2009-08-13 17:38:42

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

2024-04-07 07:49:05

C#Switch模式匹配

2009-07-31 15:44:02

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

2009-07-31 15:37:45

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

2009-09-04 11:15:07

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

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