为什么super()构造函数在这个构造函数中不起作用?它一直告诉我它必须是构造函数中的第一个语句,但即使它是第一个语句也不起作用。
我也不能使用它,当它是第一个声明时,因为在我的程序中我必须首先检查是否有足够的汽车和汽油可以出售汽车。有什么办法可以解决这个问题吗?我想我必须用这种方式,我们的教授告诉我们。
public class PassengerCarTrader extends CarTrader {
public static final int CAPACITY_AUDI = 4;
public static final int CAPACITY_BMW = 4;
public static final int CAPACITY_OPEL = 4;
public static final int CAPACITY_MERCEDES = 3;
public void sellCar(int brand){
super(brand);
Vehicle soldVehicle=null;
switch (brand){
case 1://AUDI
if (this.audiStock>=1 && this.gasStockLiters>=Vehicle.TANK_SIZE_PASSENGER_CAR){
this.audiStock = this.audiStock - 1;
} else {
System.err.println("Nicht genug AUDI oder Benzin vorhanden");
}
break;
case 2://BMW
if(this.bMWStock>=1 && this.gasStockLiters>=Vehicle.TANK_SIZE_TRUCK){
this.bMWStock = this.bMWStock -1;
} else {
System.err.println("Nicht genug BMW oder Benzin vorhanden");
}
break;
case 3://MERCEDES
if (this.mercedesStock>=1 && this.gasStockLiters>=Vehicle.TANK_SIZE_PASSENGER_CAR){
this.mercedesStock = this.mercedesStock - 1;
} else {
System.err.println("Nicht genug Mercedes oder Benzin vorhanden");
}
break;
case 4://OPEL
if(this.opelStock>=1 && this.gasStockLiters>=Vehicle.TANK_SIZE_TRUCK){
this.opelStock = this.opelStock -1;
} else {
System.err.println("Nicht genug Opel oder Benzin vorhanden");
}
break;
default: soldVehicle=null;
break;
}
}
}
这是主要的课程
public abstract class CarTrader {
public static final float CAPACITY_DIESEL_LITERS = 350.f;
public static final float CAPACITY_GAS_LITERS = 180.f;
public static final int CAPACITY_PASSENGER_CARS = 15;
public static final int CAPACITY_TRUCKS = 8;
float gasStockLiters=180.f;
float dieselStockLiters=250.f;
int passengerCarsStock=15;
int trucksStock=5;
int mANStock=4;
int scaniaStock=4;
int audiStock=4;
int bMWStock=4;
int opelStock=4;
int mercedesStock=3;
public Vehicle sellVehicle(byte vehicleType){
Vehicle soldVehicle=null;
switch (vehicleType){
case VehicleType.TRUCK:
trucksStock = trucksStock - 1;
dieselStockLiters = dieselStockLiters - Vehicle.TANK_SIZE_TRUCK;
soldVehicle = new Vehicle(VehicleType.TRUCK);
System.out.println("Fahrzeug(e) erfolgreich verkauft");
break;
case VehicleType.PASSENGER_CAR:
passengerCarsStock = passengerCarsStock - 1;
gasStockLiters = gasStockLiters - Vehicle.TANK_SIZE_PASSENGER_CAR;
soldVehicle = new Vehicle(VehicleType.PASSENGER_CAR);
System.out.println("Fahrzeug(e) erfolgreich verkauft");
break;
}
return soldVehicle;
}
答案 0 :(得分:0)
您的班级中没有构造函数。 Providing Constructors for Your Classes
答案 1 :(得分:0)
super(品牌)....这不是一个超级构造函数调用..而是一个超级方法调用。 重命名" sellCar(int brand)'在PassengerCarTrader中出售车辆(byte vehicleType)...然后超级可能在你的代码中工作..... pont是...超级在你的情况下只有你的父类具有与子类相同的方法名称才能工作你叫超级