简体   繁体   English

多态,继承

[英]Polymorphism, Inheritance

So I've copied the MountainBike, RoadBike, TestBikes class from Java's tutorials ( http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html ). 因此,我从Java的教程( http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html )中复制了MountainBike,RoadBike,TestBikes类。

I then made "Bicycle.java".. But if I try to make it execute by making it the main class, then it won't even compile (tons of illegal start of expressions.) 然后,我制作了“ Bicycle.java”。但是,如果我尝试通过使其成为主类来使其执行,那么它甚至都不会编译(大量非法的表达式开头。)

Is there anyway to make this compile along with all of the classes that inherit properties from this class? 无论如何,要与所有从此类继承属性的类一起进行编译?

   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 + ". ");
    }
}
}

You're putting methods inside of methods and in fact you've got your main method enclosing all of the code of your Bicycle class -- don't do this. 您将方法放入方法内部,实际上,您已经将主方法包含了Bicycle类的所有代码,请不要这样做。 Your main method should be its own separate method, should not hold other methods, should create a Bicycle instance. 您的main方法应该是其自己的单独方法,不应该包含其他方法,应该创建Bicycle实例。 call methods on this instance and that's about it. 在这个实例上调用方法就可以了。

public class Bicycle {
        public int cadence;
        public int gear;
        public 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 + ". ");
    }

    public static void main(String[] args) {
      Bicycle bicycle = new Bicycle(20, 10, 2);
      System.out.println(bicycle);
      // ... etc
    }
}

Next time, please show us the actual code that is causing the problem from the get-go as well as your error messages. 下次,请向我们显示从一开始就引起问题的实际代码以及您的错误消息。 You'll get much better help this way, and we'll all be a little less frustrated. 通过这种方式,您将获得更好的帮助,而我们都会感到沮丧。

Take in the previous answers and start over, try breaking up the different methods and debugging each instead of getting a wash of errors and tossing up your hands. 接受先前的答案并重新开始,尝试分解不同的方法并调试每种方法,而不要洗刷错误并举手。 Make sure each compiles as you code so you know where the problems begin. 确保每个代码在编译时都进行编译,以便您知道问题从何而来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM