无法在Java中运行我的多态性代码示例

时间:2017-08-04 11:30:42

标签: java polymorphism

我复制了一个自行车示例,并希望将其更改为汽车示例,但我无法运行PolyCar类的扩展类Mountainbike。如果我评论Mountainbike,PolyCar将运行。谁能告诉我为什么程序不识别Mountainbike

以下是用于运行我的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.");
    }
}

}

2 个答案:

答案 0 :(得分:1)

您已将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{
    ...
}

Working TutorialsPoint示例here

答案 1 :(得分:0)

您的PolyBar类范围内有MountainBike和RoadBike。

逻辑上要做的就是将它们拿出来(在MountainBike定义之前放置一个大括号,并在RoadBike类之后删除最后一个)。或者让MountainBike和RoadBike类静态并初始化它们就像bike02 = new PolyCar.MountainBike(20,10,5," Dual");

第二个选项在我看来是不好的,因为在将它们作为嵌套类放置时没有任何逻辑,因为你试图完成继承&多态和嵌套代表一种"组合"关系。