gpt4 book ai didi

java - 在运行 Java 多态性代码示例时遇到问题

转载 作者:行者123 更新时间:2023-12-02 12:25:30 25 4
gpt4 key购买 nike

我复制了一个自行车示例,并想将其更改为汽车示例,但我无法运行 PolyCar 类的扩展类 Mountainbike。如果我注释掉 Mountainbike,PolyCar 就会运行。谁能告诉我为什么该程序无法识别山地自行车

这是用于运行我的 PolyCar 程序的 TestCar 程序

//import PolyCar.MountainBike;
//import PolyCar.RoadBike;

public class TestCars {
public static void main(String[] args){
PolyCar bike01, bike02, bike03;

bike01 = new PolyCar(20, 10, 1);
bike02 = new MountainBike(20, 10, 5, "Dual");
// bike03 = new RoadBike(40, 20, 8, 23);

bike01.printDescription();
bike02.printDescription();
// bike03.printDescription();
}
}

这是我的 PolyCar 代码

public class PolyCar {

// the PolyCar class has three fields
public int cadence;
public int gear;
public int speed;

// the PolyCar class has one constructor
public PolyCar(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

// the PolyCar class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}

public void setGear(int newValue) {
gear = newValue;
}

public void applyBrake(int decrement) {
speed -= decrement;
}

public void speedUp(int increment) {
speed += increment;
}
public void printDescription(){
System.out.println("\nBike is " + "in gear " + this.gear
+ " with a cadence of " + this.cadence +
" and travelling at a speed of " + this.speed + ". ");
}
public class MountainBike extends PolyCar {
private String suspension;

public MountainBike(
int startCadence,
int startSpeed,
int startGear,
String suspensionType){
super(startCadence,
startSpeed,
startGear);
this.setSuspension(suspensionType);
}

public String getSuspension(){
return this.suspension;
}

public void setSuspension(String suspensionType) {
this.suspension = suspensionType;
}

public void printDescription() {
super.printDescription();
System.out.println("The " + "MountainBike has a" +
getSuspension() + " suspension.");
}
}
public class RoadBike extends PolyCar{
// In millimeters (mm)
private int tireWidth;

public RoadBike(int startCadence,
int startSpeed,
int startGear,
int newTireWidth){
super(startCadence,
startSpeed,
startGear);
this.setTireWidth(newTireWidth);
}

public int getTireWidth(){
return this.tireWidth;
}

public void setTireWidth(int newTireWidth){
this.tireWidth = newTireWidth;
}

public void printDescription(){
super.printDescription();
System.out.println("The RoadBike" + " has " + getTireWidth() +
" MM tires.");
}
}

}

最佳答案

您已将 MountainBikeRoadBike 封装在 PolyCar 类中;它们应该位于单独的类文件中。您的解决方案非常简单,只需在声明其他两个子类之前移动最后一个花括号来结束 PolyCar 类即可。

目前您有:

public class PolyCar
{
...
public class MountainBike extends PolyCar{
...
}

public class RoadBike extends PolyCar{
...
}
}

更改为:

class PolyCar {
...
}

class RoadBike extends PolyCar{
....
}

class RoadBike extends PolyCar{
...
}

工作教程点示例 here

关于java - 在运行 Java 多态性代码示例时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45505711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com