未定义的构造函数错误

Undefined Constructor error

我正在尝试为汽车经销商实施一个系统,但是当我尝试在派生的 class 中实例化我的 Car class 时,我收到错误消息

Multiple markers at this line
- The constructor Car(String, int, String, String, double, double) is 
 undefined

这是父class汽车:

package Number3;

public class Car {

private String plateNum;
private int year;
private String make;
private String model;
protected double costPrice;
protected double sellingPrice;

public Car()
{
    plateNum = "";
    year = 1990;
    make = "";
    model = "";
    costPrice = 0.0;
    sellingPrice = 0.0;
}

public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
{
    this.plateNum = plateNum;
    this.year = year;
    this.make = make;
    this.model = model;
    this.costPrice = costPrice;
    this.sellingPrice = sellingPrice;
}

public double getCostPrice()
{
    return costPrice;
}

public double computeSellPrice()
{
    sellingPrice = costPrice;
    return sellingPrice;
}

public void displayCarDetails()
{
    System.out.println("Plate number: "+plateNum);
    System.out.println("Year: "+year);
    System.out.println("Make: "+make);
    System.out.println("Cost price: "+costPrice);
    System.out.println("Selling price: "+sellingPrice);
}

}

和子class newCar:

package Number3;

public class newCar extends Car{

private double tax;

public newCar(String plateNum,int year, String make, double costPrice, double sellingPrice, double tax)
{
    super(plateNum,year,make,costPrice,sellingPrice); //where the error is found
    this.tax = (25/100);
}

public double computeSellPrice()
{
    sellingPrice = costPrice + (costPrice * tax);
    return sellingPrice;
}

public void displayCarDetails()
{
    super.displayCarDetails();
}
}

如有任何帮助,我们将不胜感激。

Car class 没有带 5 个参数的构造函数。

定义为

public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
{
 ...
}

并且您试图在不传递 model 参数的情况下调用它。

super(plateNum,year,make,costPrice,sellingPrice); //where the error is found

您的 Car 构造函数的签名与派生 class 中的签名不匹配。

在你的 Car class 中,这是构造函数:

public Car(String plateNum,int year,
      String make,String model,double costPrice,double sellingPrice) {
        ...
  }

String, int, String, String, double, double)

而在派生中 class:

你有:

super(plateNum,year,make,costPrice,sellingPrice)

也就是int, int, String, double, double

更改 newCar class 中对 Super 的调用参数,以匹配 Car class 的构造函数。也就是说,在您的 newCar class 行

super(plateNum,year,make,costPrice,sellingPrice)

应该是:

super(plateNum, year,
      make, model, costPrice, sellingPrice)

您的 super/parent class Car 有一个无参数构造函数 public Car() { 和以下从 sub/child 调用的 6 参数构造函数class 个构造函数使用关键字 super.

public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)

注意,它期望 String model 作为它的第四个参数,但是你的 public newCar() 构造函数只传递给它五个参数。缺少参数 model

public newCar(String plateNum,int year, String make, double costPrice, double sellingPrice, double tax)
{
    super(plateNum,year,make,costPrice,sellingPrice); // model MISSING!

因此,要修复它,要么修改构造函数以也接受 model(就像在 usedCar() 构造函数中一样),要么将 null 传递给超级 class构造函数为

super(plateNum,year,make,null,costPrice,sellingPrice); // model = null