我正在尝试将我的Vehicle类扩展为HumanPowered类 - 有一个每小时卡路里的字段。这是我第一次尝试扩展课程,所以我在这里有点困惑。
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类收到错误,上面写着“隐式超级构造函数Vehicle()未定义。必须显式调用另一个构造函数。”我无法弄清楚我在哪里出错了。感谢您的帮助!
答案 0 :(得分:3)
Vehicle
没有默认构造函数,因此你必须调用它的构造函数形式humanPowered
类,在其构造函数的第一行传递必需的参数。
public humanPowered(String aDescription, String aIdNumber, int aNumWheels, int aCalories)
{
super(aDescription,aIdNumber,aNumWheels);
...//other code
}
要记住的要点: