为什么小程序不需要 main()? [英] Why do applets not need a main()?

查看:33
本文介绍了为什么小程序不需要 main()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这适用于 Applet、Servlet、Midlet 等的子类

This applies to subclasses of Applet, Servlet, Midlet, etc.

为什么他们不需要 main()?如果我想创建一个以 init() 或类似的东西开头的 Craplet 类,这是不是设计不好,或者我该怎么做?

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?

推荐答案

实际上是好的设计,但不明显,你想做的事情没有效果,所以有点反直觉.

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.

这些类型的应用程序存在于容器中,因此它们的入口点由这些容器必须遵守的标准决定.这些标准的设计者选择不将入口点称为 main.您会将您的功能放在一个重写的方法中.所有小程序都有以下四种方法:

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:

public void init();
public void start();
public void stop();
public void destroy();

它们有这些方法是因为它们的超类 java.applet.Applet 有这些方法.

They have these methods because their superclass, java.applet.Applet, has these methods.

超类除了虚拟代码之外没有任何东西:

The superclass does not have anything but dummy code in these:

public void init() {}

如果您想派生一个类来扩展或更改init() 的名称,您应该实现您的类并让您的方法调用init().这将使用多态性让您随心所欲地调用该方法.除非您正在编写 servlet 容器,否则您很可能会浪费时间.

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.

这篇关于为什么小程序不需要 main()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆