我试图为汽车经销商实施一个系统,但是当我尝试在派生类中实例化我的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();
}
}
任何帮助都将不胜感激。
答案 0 :(得分:2)
Car构造函数的签名与派生类中的签名不匹配。
在Car
课程中,这是构造函数:
public Car(String plateNum,int year,
String make,String model,double costPrice,double sellingPrice) {
...
}
是String, int, String, String, double, double)
在派生类中:
你有:super(plateNum,year,make,costPrice,sellingPrice)
哪个是int, int, String, double, double
将Super
类中的newCar
调用中的参数更改为与Car
类的构造函数匹配。也就是说,在newCar
类中,行
super(plateNum,year,make,costPrice,sellingPrice)
应该是:
super(plateNum, year,
make, model, costPrice, sellingPrice)
答案 1 :(得分:1)
Car
类没有带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
答案 2 :(得分:1)
你的超级/父类Car
有一个无参构造函数public Car() {
和以下6参数构造函数,它是使用关键字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
传递给超类构造函数
super(plateNum,year,make,null,costPrice,sellingPrice); // model = null