How does Karel run without “main” method?

自闭症网瘾萝莉.ら 提交于 2019-12-02 02:02:32

The actual main method is somewhere else. For example, in the KarelRunner class. When java executes the program, it's actually executing the main method in the runner class. Your own run method is called from that runner code.

a "main" method is the starting point of every java program. What is going on with this class is that its not a java program by itself, is executed inside some type of framework (karel robot java implementation in this case), this frameworks off course have a "main" method but not this class, the framework knows how to load this class and execute his run method.

This "programs" are frameworks designed to do a specialized type of programs, i don't know this "karel framework", but for example, when you program a java web application you write a "servlet" but you don't write a "main" method. There are some programs called "application servers" that off course have a "main" method and take this servlet class and execute for response to some http message.

There's nothing weird about it. The CollectNewspaperKarel class is just extending the behavior of Karel. It doesn't need to have a main method.

The class that will be the entry point of the program does need to have a main method and create an instance of CollectNewspaperKarel, like:

public class MyProgram {

    public static void main(String[] args) {
        CollectNewspaperKarel cnpk = new CollectNewspaperKarel();
        cnpk.run();
    }
}

Or the instance of CollectNewspaperKarel could be a static field:

public class MyProgram {
    private static CollectNewspaperKarel cnpk = new CollectNewspaperKarel();
    public static void main(String[] args) {
        cnpk.run();
    }
}

Karel is not an application, it's an API. You make the application.

When you read all of those instructions, you will discover that the program being run by Karel is a small part of a larger system -- the larger system involves drawing the map, discovering logic errors, etc. You write things in run(), then you use one of the classes which actually runs the entire system, and one of THOSE calls run() on your program. Good luck.

The key to this is quite simple: extends Karel

The Karel class is one that implements Runnable, and has quite a few methods within it.

Without trying to go into detail about what all it does, it looks something like this:

public class Karel implements java.lang.Runnable {
    private static final int NORTH = 0;
    private static final int EAST = 1;
    private static final int SOUTH = 2;
    private static final int WEST = 3;
    private static final int INFINITE = 99999999;
    private stanford.karel.KarelWorld world;
    private int x;
    private int y;
    private int dir;
    private int beepers;

    public Karel() { /* compiled code */ }
    public void run() { /* compiled code */ }
    public void move() { /* compiled code */ }
    public void turnLeft() { /* compiled code */ }
    public boolean beepersPresent() { /* compiled code */ }
    public boolean noBeepersPresent() { /* compiled code */ }
    public boolean beepersInBag() { /* compiled code */ }
    public boolean noBeepersInBag() { /* compiled code */ }
    public boolean facingNorth() { /* compiled code */ }
    public boolean facingEast() { /* compiled code */ }
    public boolean facingSouth() { /* compiled code */ }
    public boolean facingWest() { /* compiled code */ }

    public static void main(java.lang.String[] args) { /* compiled code */ }
    protected void start() { /* compiled code */ }
    protected void start(java.lang.String[] args) { /* compiled code */ }

    ...
}

I took out quite a few methods from there because its a very long list of things it defines.

But ore that last one that I left in there... protected void start(java.lang.String[] args) (or at least, that's how my library inspector interpreted it). Digging into this code, it appears that this invokes main() in KarelProgram, but for the most part, thats neither here nor there.

And thus you have inheritance. You are inheriting a number of other methods that the code is using. That you are using move(); in that code but not defining it should be no more surprising than having a main(java.lang.String[] args) defined.

All it takes is opening up the class in the jar in your IDE.

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