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

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

    • 分享

      extends和implements

       子路問學(xué) 2012-09-27

      初學(xué)Java語言, 代碼中的extends和implements讓我感到很迷惑,現(xiàn)在終于弄明白它們之間的區(qū)別和用法了。

      [c-sharp] view plaincopy
      1. //定義一個(gè)Runner接口   
      2. public inerface Runner   
      3. {  
      4.    int ID = 1;  
      5.    void run ();  
      6. }   

       

      1. //定義一個(gè)interface Animal,它繼承于父類Runner  
      2. interface Animal extends Runner  
      3. {  
      4.    void breathe ();  
      5. }  
      [c-sharp] view plaincopy
      1. //定義Fish類,它實(shí)現(xiàn)了Animal接口的方法run()和breather()  
      2. class Fish implements Animal  
      3. {  
      4.    public void run ()    //實(shí)現(xiàn)了Animal方法run()  
      5.  {  
      6.     System.out.println("fish is swimming");  
      7.  }  
      8. public void breather()  
      9.  {  
      10.     System.out.println("fish is bubbing");     
      11.  }  
      12. }  
      13. //定義了一個(gè)抽象類LandAnimal,它實(shí)現(xiàn)了接口Animal的方法。  
      14. abstract LandAnimal implements Animal  
      15. {  
      16.     
      17.    public void breather ()  
      18.  {  
      19.     System.out.println("LandAnimal is breathing");  
      20.  }  
      21. }  
      22. //定義了一個(gè)類Student,它繼承了類Person,并實(shí)現(xiàn)了Runner接口的方法run()。  
      23. class Student extends Person implements Runner  
      24. {  
      25.     ......  
      26.     public void run ()  
      27.      {  
      28.           System.out.println("the student is running");  
      29.      }  
      30.     ......  
      31. }  
      32.    
      33. //定義了一個(gè)接口Flyer  
      34. interface Flyer  
      35. {  
      36.    void fly ();  
      37. }  
      38.    
      39. //定義了一個(gè)類Bird,它實(shí)現(xiàn)了Runner和Flyer這兩個(gè)接口定義的方法。  
      40. class Bird implements Runner , Flyer  
      41. {  
      42.    public void run ()   //Runner接口定義的方法。  
      43.     {  
      44.         System.out.println("the bird is running");  
      45.     }  
      46.    public void fly ()   //Flyer接口定義的方法。  
      47.     {  
      48.         System.out.println("the bird is flying");  
      49.     }  
      50. }  
      51.    
      52. //TestFish類  
      53. class TestFish  
      54. {  
      55.    public static void main (String args[])  
      56.     {  
      57.        Fish f = new Fish();  
      58.        int j = 0;  
      59.        j = Runner.ID;  
      60.        j = f.ID;  
      61.     }  
      62. }  

       

      接口實(shí)現(xiàn)的注意點(diǎn):

      a)實(shí)現(xiàn)一個(gè)接口就是要實(shí)現(xiàn)該接口的所有的方法(抽象類除外)。
      b)接口中的方法都是抽象的。
      c)多個(gè)無關(guān)的類可以實(shí)現(xiàn)同一個(gè)接口,一個(gè)類可以實(shí)現(xiàn)多個(gè)無關(guān)的接口。


      extends與implements的區(qū)別:

      extends 是繼承父類,只要那個(gè)類不是聲明為final或者那個(gè)類定義為abstract的就能繼承,JAVA中不支持多重繼承,但是可以用接口來實(shí)現(xiàn),這樣就用到了implements,繼承只能繼承一個(gè)類,但implements可以實(shí)現(xiàn)多個(gè)接口,用逗號分開就行了。

      比如:

      class A extends B implements C,D,E {}    (class 子類名 extends 父類名 implenments 接口名)

       

      父類與子類繼承關(guān)系上的不同:

      A a = new B(); 結(jié)果a是一個(gè)A類的實(shí)例,只能訪問A中的方法,那么又和A a = new A();有什么區(qū)別呢?

      ***********************************************************************************************

      class B extends A
      繼承過后通常會定義一些父類沒有的成員或者方法。
      A a = new B();
      這樣是可以的,上傳。
      a是一個(gè)父類對象的實(shí)例,因而不能訪問子類定義的新成員或方法。

      ***********************************************************************************************

      假如這樣定義:
      class A

      {
         int i;
         void f(){}
      }
      class B extends A

      {
          int j;
          void f(){}       //重寫
          void g(){}
      }
      然后:
      B b = new B();
      b就是子類對象的實(shí)例,不僅能夠訪問自己的屬性和方法,也能夠訪問父類的屬性和方法。諸如b.i,b.j,b.f(),b.g()都是合法的。此時(shí)b.f()是訪問的B中的f()


      A a = new B();
      a雖然是用的B的構(gòu)造函數(shù),但經(jīng)過upcast,成為父類對象的實(shí)例,不能訪問子類的屬性和方法。a.i,a.f()是合法的,而a.j,a.g()非法。此時(shí)訪問a.f()是訪問B中的f()

      ***********************************************************************************************

      A a = new B(); 這條語句,實(shí)際上有三個(gè)過程:
      (1) A a;
      將a聲明為父類對象,只是一個(gè)引用,未分配空間
      (2) B temp = new B();
      通過B類的構(gòu)造函數(shù)建立了一個(gè)B類對象的實(shí)例,也就是初始化
      (3) a = (A)temp;
      將子類對象temp轉(zhuǎn)換未父類對象并賦給a,這就是上傳(upcast),是安全的。
      經(jīng)過以上3個(gè)過程,a就徹底成為了一個(gè)A類的實(shí)例。
      子類往往比父類有更多的屬性和方法,上傳只是舍棄,是安全的;而下傳(downcast)有時(shí)會增加,通常是不安全的。

      ***********************************************************************************************

      a.f()對應(yīng)的應(yīng)該是B類的方法f()
      調(diào)用構(gòu)造函數(shù)建立實(shí)例過后,對應(yīng)方法的入口已經(jīng)確定了。
      如此以來,a雖被上傳為A類,但其中重寫的方法f()仍然是B的方法f()。也就是說,每個(gè)對象知道自己應(yīng)該調(diào)用哪個(gè)方法。
      A a1 = new B();
      A a2 = new C();
      a1,a2兩個(gè)雖然都是A類對象,但各自的f()不同。這正是多態(tài)性的體現(xiàn)。

      ***********************************************************************************************

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多