乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#關(guān)于虛方法和重寫(xiě)的實(shí)例說(shuō)明(virtual 和override)

       昵稱10504424 2013-02-20
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5.   
      6. namespace ConsoleAppsTest  
      7. {  
      8.     class Program  
      9.     {  
      10.         static void Main(string[] args)  
      11.         {  
      12.             D d = new D();  
      13.             A a = d;  
      14.             B b = d;  
      15.             C c = d;  
      16.             a.F();  
      17.             b.F();  
      18.             c.F();  
      19.             d.F();  
      20.             a.F1();  
      21.             a.F2();  
      22.             c.F1();  
      23.             c.F2();  
      24.             Console.ReadKey();  
      25.         }  
      26.     }  
      27.   
      28.     public class A  
      29.     {  
      30.         public virtual void F()  
      31.         {  
      32.             Console.WriteLine("A.F");  
      33.         }  
      34.         public void F1()  
      35.         {  
      36.             Console.WriteLine("A.F1");  
      37.         }  
      38.         public virtual void F2()  
      39.         {  
      40.             Console.WriteLine("A.F2");  
      41.         }  
      42.     }  
      43.     public class B : A  
      44.     {  
      45.         public override void F()  
      46.         {  
      47.             Console.WriteLine("B.F");  
      48.         }  
      49.     }  
      50.     public class C : B  
      51.     {  
      52.         new public virtual void F()  
      53.         {  
      54.             Console.WriteLine("C.F");  
      55.         }  
      56.         new public  void F1()  
      57.         {  
      58.             Console.WriteLine("C.F1");  
      59.         }  
      60.     }  
      61.     public class D : C  
      62.     {  
      63.         public override void F()  
      64.         {  
      65.             Console.WriteLine("D.F");  
      66.         }  
      67.     }  
      68. }  

      執(zhí)行結(jié)果如下:


      說(shuō)明:

      就 類A 來(lái)說(shuō),其他類同:

      A 是a 的聲明類;D是實(shí)例類

      在調(diào)用實(shí)例a的方法時(shí),首先是在聲明類A中找此方法,如果有此方法并且是非虛方法,就直接執(zhí)行該方法;如果有此方法并且是虛方法,就在A的子類中找,如果在子類B中找到該方法的重寫(xiě),就執(zhí)行B中的重寫(xiě)方法;如果沒(méi)有找到B中對(duì)該方法的重寫(xiě),就執(zhí)行A類的虛方法。注意:如果A a =new A();調(diào)用a的方法時(shí),都執(zhí)行A類自己的方法。

      如果有子類new 一個(gè)方法時(shí),就表示覆蓋了基類的該方法,調(diào)用時(shí)就執(zhí)行自己的new方法;

      如果子類中沒(méi)有該方法,調(diào)用基類的方法時(shí),執(zhí)行基類的方法。



        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多