2015-02-09 238 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 
相關問題