刚刚按照书上编了个小程序,写着写着就提示一个错误:Constructor call must be the first statement in a constructor。然后在网上搜了一下,原来是构造函数都要放在第一行,这是语法规则。那好吧,暂且这样考虑吧。(然后想说一句,java高手真经错误挺多的啊)
class Car{
int v;
String name;
Car(int v, String name){
this.v = v;
this.name = name;
}
Car(){
}
void drive(){
System.out.println("Car 速度:" + v);
}
}
class Bus extends Car{
int p;
Bus(){
}
Bus(int p){
this.p = p;
}
Bus(int p, int v, String name){
super(v, name);
this.p = p; //现在这里改回来了
}
void carry(){
System.out.println("Bus 载人:" + p);
}
void print(){
System.out.println("Bus 速度:" + v);
System.out.println("Bus 载人:" + p);
System.out.println("Bus 名字:" + name);
}
}