Java 未定义的构造函数错误
我正在尝试为汽车经销商实现一个系统,但是当我尝试在派生类中实例化我的Java 未定义的构造函数错误,java,oop,inheritance,Java,Oop,Inheritance,我正在尝试为汽车经销商实现一个系统,但是当我尝试在派生类中实例化我的car类时,我得到了错误消息 Multiple markers at this line - The constructor Car(String, int, String, String, double, double) is undefined 这是家长级汽车: package Number3; public class Car { private String plateNum; private int year
car
类时,我得到了错误消息
Multiple markers at this line
- The constructor Car(String, int, String, String, double, double) is
undefined
这是家长级汽车:
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类没有接受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
类没有接受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
类中,这是构造函数:
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
在newCar
类中将调用中的参数更改为Super
,以匹配Car
类的构造函数。也就是说,在您的newCar
课程中
super(plateNum,year,make,costPrice,sellingPrice)
应该是:
super(plateNum, year,
make, model, costPrice, sellingPrice)
汽车构造函数的签名与派生类中的签名不匹配 在您的
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
在newCar
类中将调用中的参数更改为Super
,以匹配Car
类的构造函数。也就是说,在您的newCar
课程中
super(plateNum,year,make,costPrice,sellingPrice)
应该是:
super(plateNum, year,
make, model, costPrice, sellingPrice)
您的超级/父类
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
您的超级/父类
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
构造函数确实不在那里-您的Super()中有一个小错误调用。你的超级类构造函数也需要一个模型参数。你不是为新车传递它,而是为二手车传递它。@RaviThapliyal你能回答这个问题吗?请举一些例子,因为我不清楚。我还没有完全掌握OOP的概念。我已经尝试过了,但它仍然不起作用。构造函数确实不是最好的re-您的Super()中有一个小错误调用。你的超级类构造函数也需要一个模型参数。你不是为新车传递它,而是为二手车传递它。@RaviThapliyal你能回答这个问题吗?用我不清楚的例子。我还没有完全掌握OOP的概念。我已经尝试过了,但仍然不起作用。这不是真的。它是-p但是,参数确实匹配。这不是真的。它确实匹配-但是参数确实匹配。在哪个派生类中它是int?如何修复它?我找不到它。实际上,在我将它修改为字符串之前,plateNum是int。只需确保您的Car类中的构造函数与您调用
super
的任何类中的构造函数匹配。不客气。再来一个事情,你的类名应该是newCar
。它在哪个派生类中是int?如何修复它?我找不到它。实际上,在我将它修改为String之前,plateNum是int。只要确保你的Car类中的构造函数与你调用super
的任何类中的构造函数相匹配。不客气。还有一件事,你的class名称newCar
应为newCar
。