java中为啥要有抽象类_在Java中为main方法创建单独的类的原因

java中为啥要有抽象类

All Java programs must have an entry point, which is always the main() method. Whenever the program is called, it automatically executes the main() method first.

所有Java程序都必须具有一个入口点,该入口点始终是main()方法。 每当调用该程序时,它都会首先自动执行main()方法。

The main() method can appear in any class that is part of an application, but if the application is a complex containing multiple files, it is common to create a separate class just for main(). The main class can have any name, although typically it will just be called "Main".

main() 方法可以出现在属于应用程序的任何类中,但是如果应用程序是一个包含多个文件的复杂类,则通常只为main()创建一个单独的类。 主类可以具有任何名称,尽管通常将其仅称为“ Main”。

主要方法有什么作用? ( What Does the Main Method Do? )

The main() method is the key to making a Java program executable. Here is the basic syntax for a main() method:

main()方法是使Java程序可执行的关键。 这是main()方法的基本语法


public class MyMainClass {
public static void main(String[] args) {
// do something here...
}
}

Note that the main() method is defined within curly braces and is declared with three keywords: public, static and void :

请注意,main()方法是在花括号内定义的,并使用三个关键字声明:public,static和void:

  • public: This method is public and therefore available to anyone.

    public :此方法是公共的,因此任何人都可以使用。

  • static: This method can be run without having to create an instance of the class MyClass.

    static :无需创建MyClass类的实例即可运行此方法。

  • void: This method does not return anything.

    void :此方法不返回任何内容。

  • (String[] args): This method takes a String argument. Note that the argument args can be anything — it's common to use "args" but we could instead call it "stringArray".

    (String [] args) :此方法使用String参数。 请注意,参数args可以是任何参数-通常使用“ args”,但我们可以改称为“ stringArray”。

Now let's add some code to the main() method so that it does something:

现在,我们将一些代码添加到main()方法中,以便执行以下操作:


public class MyMainClass {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

This is the traditional "Hello World!" program, as simple as it gets. This main() method simply prints the words "Hello World!" In a real program, however, the main() method just starts the action and does not actually perform it.

这就是传统的“ Hello World!” 程序,就这么简单。 这个main()方法仅打印单词“ Hello World!”。 但是,在实际程序中 ,main()方法只是开始操作而实际上并未执行。

Generally, the main() method parses any command line arguments, does some setup or checking, and then initializes one or more objects that continue the work of the program. 

通常,main()方法解析任何命令行参数,进行一些设置或检查,然后初始化一个或多个对象以继续程序的工作。

是否单独上课? ( Separate Class or Not? )

As the entry point into a program, the main() method has an important place, but programmers do not all agree on what it should contain and to what degree it should be integrated with other functionality.

作为程序的切入点,main()方法占有重要地位,但是程序员并不都同意它应该包含什么以及应该在什么程度上与其他功能集成。

Some argue that the main() method should appear where it intuitively belongs — somewhere at the top of your program. For example, this design incorporates main() directly into the class that creates a server:

有人认为main()方法应该显示在直观上所属的位置-在程序顶部。 例如,此设计将main()直接合并到创建服务器的类中:

However, some programmers point out that putting the main() method into its own class can help make the Java components you are creating reusable. For example, the design below creates a separate class for the main() method, thus allowing the class ServerFoo to be called by other programs or methods:

但是,一些程序员指出,将main()方法放入其自己的类中可以帮助使您正在创建的Java组件可重用。 例如,下面的设计为main()方法创建一个单独的类,从而允许其他程序或方法调用ServerFoo类:

主要方法的要素 ( Elements of the Main Method )

Wherever you place the main() method, it should contain certain elements since it is the entry point to your program. These might include a check for any preconditions for running your program.

无论将main()方法放在哪里,它都应包含某些元素,因为它是程序的入口点。 这些可能包括检查运行程序的所有前提条件。

For example, if your program interacts with a database, the main() method might be the logical place to test basic database connectivity before moving on to other functionality.

例如,如果您的程序与数据库交互,则在继续使用其他功能之前,main()方法可能是测试基本数据库连接性的逻辑位置。

Or if authentication is required, you would probably put the login information in main().

或者,如果需要身份验证,则可能会将登录信息放在main()中。

Ultimately, the design and location of main() are completely subjective. Practice and experience will help you determine where best to put main(), depending on the requirements of your program. 

最终,main()的设计和位置完全是主观的。 实践和经验将帮助您确定main()的最佳放置位置,具体取决于程序的要求。

翻译自: https://www.thoughtco.com/main-class-2034233

java中为啥要有抽象类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值