java 为什么小程序不需要 main()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/932052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do applets not need a main()?
提问by Lucky
This applies to subclasses of Applet, Servlet, Midlet, etc.
这适用于 Applet、Servlet、Midlet 等的子类。
Why do they not need a main()? If I wanted to create a Crapletclass that starts at init()or something similar, is it bad design, or how would I go about doing it?
为什么他们不需要一个main()?如果我想创建一个Craplet类,在启动init()或类似的东西,它是不好的设计,不然我怎么会去这样做呢?
回答by ojblass
It is actually good design but not obvious and what you want to do would have no effect so it is a little counter intuitive.
它实际上是一个很好的设计,但并不明显,你想做的事情不会有任何影响,所以它有点违反直觉。
These types of applications live their lives in containers and as such their entry points are determined by the standards those containers must adhere to. The designers of these standards chose not to call the entry point main. You would place your functionality in an overridden method. All applets have the following four methods:
这些类型的应用程序生活在容器中,因此它们的入口点由这些容器必须遵守的标准决定。这些标准的设计者选择不将入口点称为 main。您会将您的功能放在一个重写的方法中。所有小程序都有以下四种方法:
public void init();
public void start();
public void stop();
public void destroy();
They have these methods because their superclass, java.applet.Applet, has these methods.
它们具有这些方法是因为它们的超类java.applet.Applet具有这些方法。
The superclass does not have anything but dummy code in these:
超类除了这些虚拟代码之外什么都没有:
public void init() {}
If you want to derive a class to extend or change the name of init()you should Implement your class and have your method call init(). This would use polymorphism to let you call the method whatever you like. Unless you are writing servlet container you are likely wasting your time.
如果您想派生一个类来扩展或更改init()您的名称,您应该实现您的类并调用您的方法init()。这将使用多态性让您随心所欲地调用该方法。除非您正在编写 servlet 容器,否则您很可能是在浪费时间。
回答by kgiannakakis
Applets and Servlets do not start their own process. Instead they run inside a container. Therefore, they do no need a static main method (which starts the process), but a way to interact with their container.
Applet 和 Servlet 不会启动它们自己的进程。相反,它们在容器内运行。因此,它们不需要静态 main 方法(启动进程),而是一种与其容器交互的方法。
回答by JustJeff
'main' is just a convention that C, C++ and java commonly support, but for example, if you write C or C++ directly against the Win32 API, you don't have to have main(), but instead you have WinMain.
'main' 只是 C、C++ 和 java 通常支持的约定,但是例如,如果直接针对 Win32 API 编写 C 或 C++,则不必使用 main(),而是使用 WinMain。
回答by John Feminella
The executing environment of an applet (typically, your web browser) calls the applet methods at different points depending on what stage of rendering the it's reached. That provides a level of abstraction that's better suited for the web than a simple main()method. Further, launching arbitrary Java programs with main()methods would usually be considered something of a security risk.
小程序的执行环境(通常是您的 Web 浏览器)在不同的点调用小程序方法,具体取决于它到达的呈现阶段。这提供了比简单main()方法更适合 Web 的抽象级别。此外,使用main()方法启动任意 Java 程序通常会被视为存在安全风险。
回答by Aishwarya Hungund
Applet do not use main() because when applet is loaded it automatically calls certain methods of applet class to start and executes the applet code. and applet have its own life cycle.
Applet 不使用 main(),因为在加载 Applet 时,它会自动调用 Applet 类的某些方法来启动和执行 Applet 代码。和小程序有自己的生命周期。

