java 構造方法

來源:互聯網
上載者:User

標籤:成員變數   找不到   cto   必須   exception   隱藏   方法調用   []   str   

1. 自訂了構造方法,預設的構造方法還會建立嗎,為什嗎?

  自訂了構造方法,則不會再提供隱藏的無參構造方法。

    問題: 為什麼java 在自訂構造方法後,不在提供隱藏的構造方法呢?

       構造方法的作用是初始化成員變數,使用者自訂了構造方法自然是像建立一個有特定初始值的對象,如果此時還預設提供隱式構造方法,

        那麼外部就可以建立一個不符合作者預期的對象。

        舉個例子:

            

public class Rect {    private float width;    private float height;    public Rect(float width, float height) {        if (width <= 0 || height <= 0) {            // 參數不合理時拋出異常            throw new IllegalArgumentException(                    String.format("Size must > 0. w: %f, h: %f",                            width, height));        }        this.width = width;        this.height = height;    }}

 

 

 

2. 父子類

    問題1: 如果父類定義了一個帶參的構造方法, 此時子類也定義了帶參數的建構函式,這是會報錯 

          Implicit super constructor P() is undefined. Must explicitly invoke another constructor

          意思是父構造器沒有定義,必須明確一個其他構造器。

          原因是java 中建立子類時需要建立父類,子類會預設調用父類的隱藏構成方法。

          但是你自訂了父構造方法,此時子類就找不到預設的構造方法了。所以需要你明確指定調用哪個父構造器。

          兩種方法解決: 

            1.  顯示寫出父類預設構造方法 

            2. 在子類構造器方法中明確調用父類哪個構造器 如  super(name);

          

package test;方案1 : class P{static {System.out.println("P static");}{System.out.println("P con");}public P(){System.out.println("預設p");}public P(String name){System.out.println("帶參p");}}class C extends P{static {System.out.println("C static");}{System.out.println("C con");}public C(){}public C(String name){System.out.println("帶參c");}}public class Test5 {public static void main(String[] args) {C c = new C("張三");}}

  

方案2 : package test;class P{static {System.out.println("P static");}{System.out.println("P con");}public P(String name){System.out.println("帶參p");}}class C extends P{static {System.out.println("C static");}{System.out.println("C con");}public C(){super("");}public C(String name){super(name);System.out.println("帶參c");}}public class Test5 {public static void main(String[] args) {C c = new C("張三");}}

  

          

          

    3. 建立子類對象 調用順序

      

package test;class P{    static {System.out.println("P static");}    {        System.out.println("P con");    }        public P(String name){        System.out.println("帶參p");    }}class C extends P{    static {System.out.println("C static");}    {        System.out.println("C con");    }    public C(){        super("");    }    public C(String name){        super(name);        System.out.println("帶參c");    }}public class Test5 {    public static void main(String[] args) {        C c = new C("張三");    }}

結果:  

  P static
  C static
  P con
  帶參p
  C con
  帶參c

    所以執行順序是: 父靜態塊 - 子靜態塊  -  父構造塊 - 父構造方法 -  子構造塊 -  子構造方法 

    注意: 父構造方法調用哪個問題:  

        如果沒有指定那麼就是預設 構造方法 

        如果指定了(如本例 super(name)  ), 則是 調用 父類的  帶參構造方法 。。。 

 

 

 

 

 

 

 

    

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.