gpt4 book ai didi

java - 未定义的构造函数错误

转载 作者:行者123 更新时间:2023-12-01 18:18:06 24 4
gpt4 key购买 nike

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

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

这是父类 Car:

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);
}

}

和子类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 构造函数的签名与派生类中的签名不匹配。

在您的 Car 类中,这是构造函数:

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

它是字符串,int,字符串,字符串, double , double )

在派生类中:

你有:

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

这是int、int、String、double、double

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

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

应该是:

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

关于java - 未定义的构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28416573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com