Subclass not allowing world to run

回眸只為那壹抹淺笑 提交于 2019-12-12 01:29:10

问题


I am a beginner, taking a CompSci class in school. This chapter is making subclasses; however, The class is somehow causing the program to terminate. The world doesn't even appear, and there are no errors. (I am running Eclipse).

Here is my code:

package karel;
import kareltherobot.*;
public class Race implements Directions
{
public static void main(String args[]) {



class Car extends UrRobot
{   public Car(int street, int avenue, Direction direction, int beepers) 
    {super(5, 5, East, infinity);
    }

    public void turnAround()
    {
        turnLeft();
        turnLeft();
    }

    public void turnRight()
    {   
        turnLeft();
        turnLeft();
        turnLeft();
    }
}
    {   
        World.setVisible(true);
        World.showSpeedControl(true);
    }
        {
            Car kar = (Car) new UrRobot(5, 5, East, infinity);
            kar.move();
            kar.turnLeft();
            kar.move();
            kar.turnAround();
            kar.move();
            kar.turnRight();

        }

}

}

Is there a way to get this program to not error to termination?


回答1:


You need to have code ran in the main function. Java finds and executes code in the classes main function. Try that, if it doesn't work, come back and repost.




回答2:


Try the following.

import kareltherobot.*;

public class Race implements Directions
{
    public Race(){
     }

public static void main(String args[]) {

class Car extends UrRobot
{   

public Car(int street, int avenue, Direction direction, int beepers)

{

    super(street, avenue, direction, beepers);
}

public void turnAround()

{
    super.turnLeft();
    super.turnLeft();
}
public void turnRight()
{           
    super.turnLeft();
    super.turnLeft();
    super.turnLeft();
}
}

World.setVisible(true);
World.showSpeedControl(true);    
Car kar = new Car(5, 5, East, 100);
kar.move();
kar.turnLeft();
kar.move();
kar.turnAround();
kar.move();
kar.turnRight();

}
}


来源:https://stackoverflow.com/questions/12232786/subclass-not-allowing-world-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!