简体   繁体   English

为什么我的 java 程序在没有 eclipse 中的 main(String[] args) 方法的情况下运行?

[英]Why does my java program run without the main(String[] args) method in eclipse?

import java.awt.Color;
import acm.graphics.*;
import acm.program.*;

public class ColHelloWorld extends GraphicsProgram {
    public void run() {
        GLabel label = new GLabel("Hello World", 200, 75);
        label.setFont("SansSerif-96");
        label.setColor(Color.RED);
        add(label);

        println("Hello dumbass");
    }
}

So I have this sample code which I can run in eclipse by going to Run As option for the project and selecting run as " Java Application ".所以我有这个示例代码,我可以在 eclipse 中运行,方法是转到项目的运行方式选项并选择运行为“ Java 应用程序”。 My question is why does this compile and run even though it doesn't have a main() method call.我的问题是为什么即使它没有 main() 方法调用,它也会编译和运行。 My guess is this works because of the acm library I imported and extending the GraphicsProgram class that already has a main() method but I am still a little confused as to why it works and any clarification would be helpful.我的猜测是因为我导入了 acm 库并扩展了已经具有 main() 方法的 GraphicsProgram class ,但我仍然对它的工作原理有点困惑,任何澄清都会有所帮助。 Also, if I wanted to add a main() method to the above code and still keep the run() method within it, how would I go about implementing it?另外,如果我想在上面的代码中添加一个 main() 方法并仍然保留 run() 方法,我将如何 go 来实现它? I can give more details if you need any, thank you for answering.如果有需要我可以提供更多细节,谢谢你的回答。

GraphicsProgram extends Program which contains the following documentation; GraphicsProgram扩展了包含以下文档的Program

In many programming environments, objects that are specific instances of a Program subclass will run automatically without any special action on your part.在许多编程环境中,作为 Program 子类的特定实例的对象将自动运行,而无需您进行任何特殊操作。 For maximum portability, you might want to define a static main method as described in the comments for the standard implementation of main.为了获得最大的可移植性,您可能需要定义一个 static main 方法,如 main 标准实现的注释中所述。

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

相关问题 为什么 Java main() 方法接受 String args 数组? - Why does Java main() method accept an array of String args? 是否所有要在 java 程序中运行的代码都需要公开 static void main(String[] args)? - Does all code to be run in a java program need to be in public static void main(String[] args)? 运行没有主要方法的Java程序? - Run a Java program without main method? java中的main方法为什么接受无效的String args - main method in java why Accept invalid String args 为什么 java 中的主要方法必须是(字符串。[]args)? - Why main method in java has to be (String. []args)? 为什么Java程序需要“ main()”方法? - Why does a Java program require a “main()” method? 什么是“字符串参数 []”? 主要方法中的参数 Java - What is "String args[]"? parameter in main method Java Java:是否可以在程序中运行给定方法而无需main发出指令? - Java: Can I run a given method in my program without invocating from main? 在Eclipse内部Android项目运行带有main(String [] args)的Java类,因为Java应用程序导致“java.lang.String的值无效布局” - Inside Eclipse Android project run Java classes with main(String[] args) as Java Application causes “Invalid layout of java.lang.String at value” 没有主方法的Java程序 - Java program without main method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM