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

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

问题描述

这适用于小程序,Servlet中,MIDlet时,等。

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

为什么他们不需要的main()?如果我想创建一个 Craplet 类,它开始于的init()或类似的东西,它是坏的设计,或我怎么会去这样做?

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.

这些类型的应用过自己的生活容器,因此他们的切入点是由标准来确定这些容器一定要坚持。这些标准的设计人员选择不调用入口点为主。你会把你的功能在重写的方法。所有小程序有以下四种方法:

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中的,有这些方法。

超类没有什么,但假人code在这些:

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

public void 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天全站免登陆