[英]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.