繁体   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