为什么小程序不需要 main()?
这适用于Applet、Servlet、Midlet 等的子类。
为什么它们不需要main()
? 如果我想创建一个从 init()
或类似的东西开始的 Craplet
类,这是否是糟糕的设计,或者我将如何去做?
This applies to subclasses of Applet, Servlet, Midlet, etc.
Why do they not need a main()
? If I wanted to create a Craplet
class that starts at init()
or something similar, is it bad design, or how would I go about doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它实际上是很好的设计,但并不明显,你想做的事情不会有任何效果,所以它有点违反直觉。
这些类型的应用程序生活在容器中,因此它们的入口点由这些容器必须遵守的标准决定。 这些标准的设计者选择不将入口点称为 main。 您可以将您的功能放置在重写的方法中。 所有 applet 都具有以下四种方法:
它们具有这些方法是因为它们的超类
java.applet.Applet
具有这些方法。超类除了这些虚拟代码之外没有任何内容:
如果您想派生一个类来扩展或更改
init()
的名称,您应该实现您的类并让您的方法调用init ()
。 这将使用多态性让您可以随意调用该方法。 除非您正在编写 servlet 容器,否则您可能会浪费时间。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:
They have these methods because their superclass,
java.applet.Applet
, has these methods.The superclass does not have anything but dummy code in these:
If you want to derive a class to extend or change the name of
init()
you should Implement your class and have your method callinit()
. 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.Applet 和 Servlet 不会启动自己的进程。 相反,它们在容器内运行。 因此,它们不需要静态主方法(启动进程),而是需要一种与容器交互的方法。
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.
“main”只是 C、C++ 和 java 通常支持的约定,但例如,如果您直接针对 Win32 API 编写 C 或 C++,则不必使用 main(),而是使用 WinMain。
'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.
小程序的执行环境(通常是您的 Web 浏览器)会在不同的点调用小程序方法,具体取决于它所达到的渲染阶段。 这提供了比简单的
main()
方法更适合 Web 的抽象级别。 此外,使用main()
方法启动任意 Java 程序通常会被视为存在安全风险。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 withmain()
methods would usually be considered something of a security risk.Applet 不使用 main(),因为当 Applet 加载时,它会自动调用 Applet 类的某些方法来启动并执行 Applet 代码。
而applet有自己的生命周期。
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.