java繼承分析

來源:互聯網
上載者:User

標籤:

 1 class Mem 2 { 3     public int i; 4     public Mem(int i) 5     { 6         this.i = i; 7         System.out.println("create Mem: " + i); 8     } 9 }10 class A11 {12     public Mem mem = new Mem(9);13     14     public A()15     {16         System.out.println("-------------------------");17         System.out.println("create A");18         System.out.println(this + "  " + this.mem + "  " + this.mem.i);19         mem = new Mem(99);20         System.out.println(this + "  " + this.mem + "  " + this.mem.i);21         change();22         System.out.println("-------------------------");23     }24     public void change()25     {26         mem = new Mem(77);27     }28 }29 class B extends A30 {31     public B()32     {33         System.out.println("**************************************");34         System.out.println("create B");35         System.out.println(this + "  " + this.mem + "  " + this.mem.i);36         super.change();37         System.out.println(this + "  " + this.mem + "  " + this.mem.i);38         System.out.println("      " + Integer.toHexString(super.hashCode()) + "  " + super.mem + "  " + super.mem.i);39         System.out.println("**************************************");40     }41     public void change()42     {43         mem = new Mem(88);44     }45 }46 public class Test47 {48     public static void main(String[] args)49     {50         new B();        51     }52 }53 54 /*output:s55 56 create Mem: 957 -------------------------58 create A59 [email protected]  [email protected]  960 create Mem: 9961 [email protected]  [email protected]  9962 create Mem: 8863 -------------------------64 **************************************65 create B66 [email protected]  [email protected]  8867 create Mem: 7768 [email protected]  [email protected]  7769       15db9742  [email protected]  7770 **************************************71 72  */

 

 

 

 

 1 package org.tone; 2  3 class Mem 4 { 5     public int i; 6     public Mem(int i) 7     { 8         this.i = i; 9         System.out.println("create Mem: " + i);10     }11 }12 class A13 {14     public Mem mem = new Mem(9);15     16     public A()17     {18         System.out.println("-------------------------");19         System.out.println("create A");20         System.out.println(this + "  " + this.mem + "  " + this.mem.i);21         mem = new Mem(99);22         System.out.println(this + "  " + this.mem + "  " + this.mem.i);23         change();24         System.out.println("-------------------------");25     }26     public void change()27     {28         mem = new Mem(77);29     }30 }31 class B extends A32 {33 //  此處定義mem沒有初始化,可以在父類建構函式中初始化該變數, 如果在此處或者B類構造方法中初始化了, 則會覆蓋父類構造中的初始化;34 //  此處定義的mem與父類中定義的mem都同時存在於B類中, 其實B類中的super和this指向的是同一個對象(他們的hashcode值是一樣的), 35 //  只是如果類屬性或方法重名的話,通過他們訪問屬性時,他們會分別找到不同類中定義的屬性或方法36     public Mem mem; 37     public B()38     {39         System.out.println("**************************************");40         System.out.println("create B");41         System.out.println(this + "  " + this.mem + "  " + this.mem.i);42         super.change();43         System.out.println(this + "  " + this.mem + "  " + this.mem.i);44         System.out.println("      " + Integer.toHexString(super.hashCode()) + "  " + super.mem + "  " + super.mem.i);45         System.out.println("**************************************");46     }47     public void change()48     {49         mem = new Mem(88);50     }51 }52 public class Test53 {54     public static void main(String[] args)55     {56         new B();        57     }58 }59 60 /*output:s61 62 create Mem: 963 -------------------------64 create A65 [email protected]  [email protected]  966 create Mem: 9967 [email protected]  [email protected]  9968 create Mem: 8869 -------------------------70 **************************************71 create B72 [email protected]  [email protected]  8873 create Mem: 7774 [email protected]  [email protected]  8875       15db9742  [email protected]  7776 **************************************77 78 **************************************79 80  */

 

java繼承分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.