java繼承 初始化順序

來源:互聯網
上載者:User
package Study.Initialization;class Base{private String str1=init("str1");private String str2=init("str2");public static String str3=init("static str3");public Base(String str1,String str2){System.out.println("Run Construction Base");this.str1=str1;this.str2=str2;}protected static String init(String str){System.out.println(str);return str+" init";}}class Derived extends Base{    private String str4=init("str4");    public static String str5=init("static str5");    public Derived(String str1,String str2,String str4){    super(str1,str2);    System.out.println("Run Construction Derived");    this.str4=str4;    }}public class extendInitialization {public static void main(String[] args) {System.out.println("run extendInitialization.main()");Derived d=new Derived("construction str1","construction str2","construction str4"); System.out.println("----------------------------------------");Derived d2=new Derived("construction str11","construction str22","construction str44"); }}

運行結果:

run extendInitialization.main()
static str3
static str5
str1
str2
Run Construction Base
str4
Run Construction Derived
----------------------------------------
str1
str2
Run Construction Base
str4
Run Construction Derived
總結:
   Derived的初始化順序:
    1.按定義時的順序初始化基類的static成員
    2..按定義時的順序初始化擴充類的static成員
    (以上兩條是對第一次定義擴充類對象時運行)
    3.按定義時順序執行基類的指定初始化
    4.執行基類的建構函式 
    5 按定義時的順序執行擴充類的指定初始化
    6.行擴充類的建構函式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.