问答详情
源自:12-2 项目问题解析 1

子类名称提示错误,不知道为什么,求大神帮看看

主程序

package app;

public class App {

public static void main(String[] args) throws Exception {

Auto audi = new Auto(1,"Audi A4",500,4,0);

}

}


父类

package app;

public class Auto {

int num;

String name;

int price;

int passengers;

int carry;

public  Auto(int newnum,String newname,int newprice,int newpassengers,int newcarry) {

num = newnum;

name = newname;

price = newprice;

passengers = newpassengers;

carry = newcarry;

System.out.println(num+".  "+name+"  "+price+" yuan/perday   passenge:"+passengers);

}

}


子类

package app;

public class AudiA4 extends Auto{

}

子类名称提示错误:

Implicit super constructor Auto() is undefined for default constructor. Must define an explicit constructor


这是为什么?

提问者:刘小信 2019-08-15 15:17

个回答

  • Paladin_c
    2019-08-15 17:59:21

    类已经定义了一个有参的构造函数并且父类中没有默认的无参构造方法,此时编译器不会为你调用默认的构造函数 
    当子类继承时,必须在自己的构造函数显式调用父类的构造函数,自己才能确保子类在初始化前父类会被实例化