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

标签 java oop inheritance

我正在尝试为汽车经销商实现一个系统,但是当我尝试在派生类中实例化我的 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/

相关文章:

java - 我们是否应该始终定义接口(interface)和抽象类,即使只有 1 个类扩展它们?

java - 使用基于 OW2 ASM 事件的方法合并类

java - 将 OOP java 转换为现代 C++ 代码、抽象方法和接口(interface)方法

java - 为什么URLClassLoader不加载类?

php - MySQL 多条件类型

android - Android Activity 类中的面向对象范围和继承

java - 如何先按键再按值对 HashMap 进行排序?

c++ - 如何在 C++ 控制台游戏中表示世界和动态对象?

java - 使用Optional.ofNullable()简化条件

Java 泛型编译错误