簡體   English   中英

在子類中調用超級構造函數?

[英]Calling the super constructor in a subclass?

我的代碼

你如何調用子類中的超類構造函數來使這段代碼工作?

class Ale {

   protected int x = 1;

   public Ale( int xx ) {
      x = xx;
   }
}  

class Bud extends Ale {

   private int y = 2;

   public void display() {
      System.out.println("x = " + x + " y = " + y);             
   }
}

你可以像這樣調用超級構造函數,

class Ale {

    protected int x = 1;

    public Ale(int xx) {
        x = xx;
    }
}

class Bud extends Ale {

    Bud() {
        super(75);
    }

    private int y = 2;

    public void display() {
        System.out.println("x = " + x + " y = " + y);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM