【问题标题】:How does Karel run without "main" method?没有“主要”方法,卡雷尔如何运行?
【发布时间】:2014-07-26 22:18:14
【问题描述】:

我想知道像Karel the Robot 这样的程序运行,因为它没有使用 main() 方法。相反,它使用了 run() 方法:

import stanford.karel.Karel;

public class CollectNewspaperKarel extends Karel { 
  public void run() { 
    move(); 
  } 
}

它是如何工作的?

【问题讨论】:

    标签: java karel


    【解决方案1】:

    实际的 main 方法在其他地方。例如,the KarelRunner class。 java执行程序时,其实是在执行runner类中的main方法。您自己的 run 方法是从该运行程序代码中调用的。

    【讨论】:

      【解决方案2】:

      “main”方法是每个 java 程序的起点。这个类的情况是它本身不是一个java程序,是在某种类型的框架内执行的(在这种情况下是karel机器人java实现),这个框架当然有一个“main”方法但没有这个类,框架知道如何加载这个类并执行他的run方法。

      这个“程序”是设计用于执行特殊类型程序的框架,我不知道这个“karel 框架”,但是例如,当你编写一个 java web 应用程序时,你编写了一个“servlet”,但你不知道t 编写“主要”方法。有一些称为“应用程序服务器”的程序当然有一个“主”方法,并采用这个 servlet 类并执行以响应一些 http 消息。

      【讨论】:

        【解决方案3】:

        没有什么奇怪的。 CollectNewspaperKarel 类只是扩展了 Karel 的行为。 不需要main 方法。

        将成为程序入口点的类确实需要具有main 方法并创建CollectNewspaperKarel 的实例,例如:

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

        或者 CollectNewspaperKarel 的实例可以是静态字段:

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

        Karel 不是一个应用程序,它是一个 API。您提出申请。

        【讨论】:

          【解决方案4】:

          当您阅读所有这些说明时,您会发现 Karel 运行的程序只是更大系统的一小部分——更大的系统涉及绘制地图、发现逻辑错误等。您在 @ 中编写内容987654321@,然后您使用实际运行整个系统的类之一,并且其中一个在您的程序上调用run()。祝你好运。

          【讨论】:

            【解决方案5】:

            关键很简单:extends Karel

            Karel 类是一个实现Runnable 的类,其中包含很多方法。

            如果不详细说明它的作用,它看起来像这样:

            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 */ }
            
                ...
            }
            

            我从那里取出了很多方法,因为它定义的东西很长。

            但是我留下的最后一个……protected void start(java.lang.String[] args)(或者至少,我的图书馆检查员是这样解释的)。 深入研究这段代码,它似乎调用了 KarelProgram 中的main(),但在大多数情况下,这既不是这里也不是那里。

            因此你有继承权。您正在继承代码正在使用的许多其他方法。您在该代码中使用 move(); 但未定义它应该不会比定义 main(java.lang.String[] args) 更令人惊讶。

            只需在 IDE 中打开 jar 中的类。

            【讨论】:

            • public static void main(java.lang.String[] args) { /* 编译代码 */ } 里面是什么?
            • 不知道,我对该方法的看法是编译字节码,其中有一个invokestatic。我真的没有费心去深入研究,而且 .jar 文件中没有附加源代码。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多