所以我从 Java 的教程 ( http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
) 中复制了 MountainBike、RoadBike、TestBikes 类。
然后我制作了“Bicycle.java”..但是如果我尝试通过使其成为主类来使其执行,那么它甚至不会编译(大量非法的表达式开始。)
无论如何要使它与从该类继承属性的所有类一起编译?
public class Bicycle {
public static void main(String[] args) {
int cadence;
int gear;
int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
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 + ". ");
}
}
}