需要帮助找出错误
Need help finding the error
我有以下代码,其中包含 运行 时间错误。该代码旨在打印出:
车辆mode:flightFuel:propane最大Altitude:10000
车辆mode:traversalFuel:coalHorsepower:5000
我自己找不到它(因为我对编码还很陌生),如果可能的话希望得到一些帮助。
谢谢。
class Main {
public static void main(String[] args) {
HotAirBalloon airbag = new HotAirBalloon(10000);
Locomotive loco = new Locomotive(5000);
System.out.println(airbag.toString());
System.out.println(loco.toString());
}
}
class Vehicle {
String mode, fuel;
public String toString() {
return "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
}
class HotAirBalloon extends Vehicle {
int maxAltitude;
HotAirBalloon(int _alt) {
mode = "flight";
fuel = "propane";
maxAltitude = _alt;
}
public String toString() {
return toString() + " Max Altitude:" + maxAltitude;
}
}
class Locomotive extends Vehicle {
int horsePower;
Locomotive(int _hp) {
mode = "traversal";
fuel = "coal";
horsePower = _hp;
}
public String toString() {
return toString() + " Horsepower:" + horsePower;
}
}
因为您正在尝试调用当前方法的超级 类 版本,您需要添加 super.toString()
//old
return toString() + " Horsepower:" + horsePower;
//new
return super.toString() + " Horsepower:" + horsePower;
您还需要对其他子类执行此操作
当您调用自己的方法时,它称为递归,其中方法会不断调用自己,直到达到特定条件。
此代码可以正常工作。问题是您多次调用 toString() 导致 Stack 溢出。另外,您必须在父 class 车辆中声明一个字符串,并在子 classes 中使用飞行模式等更新它。运行 下面的代码:
class Main {
public static void main(String[] args) {
HotAirBalloon airbag = new HotAirBalloon(10000);
Locomotive loco = new Locomotive(5000);
System.out.println(airbag.toString());
System.out.println(loco.toString());
}
}
class Vehicle {
String mode, fuel;
String s;
}
class HotAirBalloon extends Vehicle {
int maxAltitude;
HotAirBalloon(int _alt) {
mode = "flight";
fuel = "propane";
maxAltitude = _alt;
s= "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
public String toString() {
return s + " Max Altitude:" + maxAltitude;
}}
class Locomotive extends Vehicle {
int horsePower;
Locomotive(int _hp) {
mode = "traversal";
fuel = "coal";
horsePower = _hp;
s= "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
public String toString() {
return s+ " Horsepower:" + horsePower;
}
}
我有以下代码,其中包含 运行 时间错误。该代码旨在打印出:
车辆mode:flightFuel:propane最大Altitude:10000
车辆mode:traversalFuel:coalHorsepower:5000
我自己找不到它(因为我对编码还很陌生),如果可能的话希望得到一些帮助。
谢谢。
class Main {
public static void main(String[] args) {
HotAirBalloon airbag = new HotAirBalloon(10000);
Locomotive loco = new Locomotive(5000);
System.out.println(airbag.toString());
System.out.println(loco.toString());
}
}
class Vehicle {
String mode, fuel;
public String toString() {
return "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
}
class HotAirBalloon extends Vehicle {
int maxAltitude;
HotAirBalloon(int _alt) {
mode = "flight";
fuel = "propane";
maxAltitude = _alt;
}
public String toString() {
return toString() + " Max Altitude:" + maxAltitude;
}
}
class Locomotive extends Vehicle {
int horsePower;
Locomotive(int _hp) {
mode = "traversal";
fuel = "coal";
horsePower = _hp;
}
public String toString() {
return toString() + " Horsepower:" + horsePower;
}
}
因为您正在尝试调用当前方法的超级 类 版本,您需要添加 super.toString()
//old
return toString() + " Horsepower:" + horsePower;
//new
return super.toString() + " Horsepower:" + horsePower;
您还需要对其他子类执行此操作
当您调用自己的方法时,它称为递归,其中方法会不断调用自己,直到达到特定条件。
此代码可以正常工作。问题是您多次调用 toString() 导致 Stack 溢出。另外,您必须在父 class 车辆中声明一个字符串,并在子 classes 中使用飞行模式等更新它。运行 下面的代码:
class Main {
public static void main(String[] args) {
HotAirBalloon airbag = new HotAirBalloon(10000);
Locomotive loco = new Locomotive(5000);
System.out.println(airbag.toString());
System.out.println(loco.toString());
}
}
class Vehicle {
String mode, fuel;
String s;
}
class HotAirBalloon extends Vehicle {
int maxAltitude;
HotAirBalloon(int _alt) {
mode = "flight";
fuel = "propane";
maxAltitude = _alt;
s= "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
public String toString() {
return s + " Max Altitude:" + maxAltitude;
}}
class Locomotive extends Vehicle {
int horsePower;
Locomotive(int _hp) {
mode = "traversal";
fuel = "coal";
horsePower = _hp;
s= "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
public String toString() {
return s+ " Horsepower:" + horsePower;
}
}