首页 > 解决方案 > 在子类中调用超级构造函数?

问题描述

我的代码

如何在子类中调用超类构造函数来使这段代码工作?

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);             
   }
}

标签: javainheritanceconstructorsubclasssuper

解决方案


您可以像这样调用超级构造函数,

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);
    }
}

推荐阅读