2014-04-26 199 views
-1

我試圖將我的Vehicle類擴展爲HumanPowered類 - 每小時有一個卡路里字段。這是我第一次嘗試擴展課程,所以我在這裏有點困惑。Java:擴展類

class Vehicle 
{ 
String description; 
String idNumber; 
int numWheels; 

public Vehicle(String aDescription, String aIdNumber, int aNumWheels) 
{ 
    description = aDescription; 
    idNumber = aIdNumber; 
    numWheels = aNumWheels; 
} 
void setDescription (String aDescription) 
{ 
    description = aDescription; 
} 
void setIdNumber (String aIdNumber) 
{ 
    idNumber = aIdNumber; 
} 
void setNumWheels (int aNumWheels) 
{ 
    numWheels = aNumWheels; 
} 
public String getDescription() 
{ 
    return description; 
} 
public String getIdNumber() 
{ 
    return idNumber; 
} 
public int getNumWheels() 
{ 
    return numWheels; 
} 
public String toString() 
{ 
    String result= String.format("ID: %s Description: %s Wheels: %d",idNumber,description,numWheels); 
    return result; 
} 

} 
class humanPowered extends Vehicle 
{ 
int calories; 
public humanPowered(String aDescription, String aIdNumber, int aNumWheels, int aCalories) //Error here 
{ 
    description = aDescription; 
    idNumber = aIdNumber; 
    numWheels = aNumWheels; 
    calories = aCalories; 
} 
void setCalories (int aCalories) 
{ 
    calories = aCalories; 
} 
public int getCalories() 
{ 
    return calories; 
} 
public String toString() 
{ 
    String result= String.format("ID: %s Description: %s Wheels: %d Calories per Hour: %d",idNumber,description,numWheels, calories); 
    return result; 
} 
} 

我得到說我的構造上面標記爲我humanPowered類錯誤「隱超構造車輛()是不確定的。必須明確地調用另一個構造。」我無法弄清楚我哪裏會出錯。感謝您的幫助!

+1

複製完整的錯誤信息,粘貼它進入谷歌,點擊你得到的第一個結果。你走了,你有一個答案。 –

回答

3

Vehicle沒有默認構造函數因此您必須調用它的構造函數形式humanPowered類在其構造函數的第一行傳遞必需的參數。

public humanPowered(String aDescription, String aIdNumber, int aNumWheels, int aCalories)  
{ 
    super(aDescription,aIdNumber,aNumWheels); 
    ...//other code 
} 

要記住的要點:

  • 每類有默認構造函數是無參數的構造
  • 如果類創建一個構造函數傳遞參數,然後通過默認構造函數不會創建
  • 每個構造默認情況下調用其超類的默認構造函數