刘小信
2019-08-15 15:17
主程序
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
这是为什么?
类已经定义了一个有参的构造函数并且父类中没有默认的无参构造方法,此时编译器不会为你调用默认的构造函数
当子类继承时,必须在自己的构造函数显式调用父类的构造函数,自己才能确保子类在初始化前父类会被实例化
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题