2015-02-09 249 views
-1

想实现一个汽车经销商的系统,但是当我尝试实例在派生类中我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(); 
} 
} 

任何帮助,将不胜感激。

+0

构造确实不那里 - 你的Super()调用中有一个小错误。 – chsh 2015-02-09 18:21:39

+2

您的超类构造函数也需要模型参数。你没有通过它的新车,但你这样做的二手车。 – 2015-02-09 18:21:46

+0

@RaviThapliyal你能回答这个问题吗?举例说明,因为我不清楚。我还没有完全掌握OOP的概念。 – 2015-02-09 18:24:07

回答

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

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

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

应该是:

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

在哪个派生类中它是int?以及如何解决它?我找不到它。其实plateNum是int,然后将其修改为String。 – 2015-02-09 18:41:11

+0

只要确保您的Car类中的构造函数与您调用'super'的任何类中的构造函数相匹配。 – 2015-02-09 18:42:42

+0

是的,我做到了,它的工作原理。谢谢。 – 2015-02-09 18:46:26

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 
+0

这是不正确的。它确实 - 参数确实匹配,但是。 – colti 2015-02-09 18:22:26

+0

不,我已经尝试过,它仍然无法正常工作。 – 2015-02-09 18:27:16

+0

你怎么称呼它? – 2015-02-09 18:28:38

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 
相关问题