使用多个主类

Normally at the outset of learning the Java programming language, there will be a number of code examples that are useful to compile and run to fully understand them. When using an IDE like NetBeans it's easy to fall into the trap of creating a new project every time for each new piece of code. However, it can all happen in one project.

通常,在学习Java编程语言之初,就会有许多代码示例可用于编译和运行以充分理解它们。 当使用NetBeans之类的IDE时,很容易陷入每次为每段新代码创建一个新项目的陷阱。 但是,所有这些都可能在一个项目中发生。

创建代码示例项目 ( Creating a Code Example Project )

A NetBeans project contains the classes needed to build a Java application. The application uses the main class as the starting point for the execution of the Java code. In fact, in a new Java application project created by NetBeans only one class included - the main class contained within the Main.java file. Go ahead and make a new project in NetBeans and called it CodeExamples.

NetBeans项目包含构建Java应用程序所需的类。 该应用程序将主类用作执行Java代码的起点。 实际上,在由NetBeans创建的新Java应用程序项目中,仅包含一个类-Main.java文件中包含的主类。 继续,在NetBeans中创建一个新项目,并将其称为CodeExamples

Let's say I want to try programming some Java code to output the result of adding 2 + 2. Put the following code into the main method:

假设我想尝试编写一些Java代码来输出加2 + 2的结果。将以下代码放入main方法中:


int result = 2 + 2;
int结果= 2 + 2;
System.out.println(result);
System.out.println(结果);
}
}

When the application is compiled and executed the output printed is "4". Now, if I want to try out another piece of Java code I have two choices, I can either overwrite the code in the main class or I can put it in another main class.

编译并执行应用程序时,输出的输出为“ 4”。 现在,如果我想尝试另一段Java代码,我有两种选择,我可以覆盖主类中的代码,也可以将其放在另一个主类中。

多个主要班级 ( Multiple Main Classes )

NetBeans projects can have more than one main class and it's easy to specify the main class an application should run. This allows a programmer to switch between any number of main classes within the same application. Only the code in one of the main classes will be executed, effectively making each class independent of each other.

NetBeans项目可以具有多个主类,并且很容易指定应用程序应运行的主类。 这允许程序员在同一应用程序中的任意数量的主类之间切换。 仅执行主类之一中的代码,从而有效地使每个类彼此独立。

Note: This is not usual in a standard Java application. All it needs is one main class as a starting point for the execution of the code. Remember this is a tip for running multiple code examples within one project.

注意:这在标准Java应用程序中并不常见。 它所需要的只是一个主类,作为执行代码的起点。 请记住,这是在一个项目中运行多个代码示例的技巧。

Let's add a new main class to the CodeSnippets project. From the File menu choose New File. In the New File wizard pick the Java Main Class file type (it's in the Java category). Click Next. Name the file example1 and click Finish.

让我们向CodeSnippets项目添加一个新的主类。 从“ 文件”菜单中选择“ 新建文件” 。 在“ 新建文件”向导中,选择Java Main Class文件类型(在Java类别中)。 单击下一步 。 将文件命名为example1 ,然后单击Finish

In the example1 class add the following code to the main method:

example1类中,将以下代码添加到main方法中


System.out.println("Four");
System.out.println(“ Four”);
}
}

Now, compile and run the application. The output will still be "4". This is because the project is still set up to use the Main class as it's main class.

现在,编译并运行该应用程序。 输出仍将为“ 4”。 这是因为该项目仍设置为使用Main类作为其主类。

To change the main class being used, go to the File menu and choose Project Properties. This dialog gives all the options that can be changed in a NetBeans project. Click on the Run category. On this page, there is a Main-Class option. Currently, it is set to codeexamples.Main (i.e., the Main.java class). By clicking the Browse button to the right, a pop-up window will appear with all the main classes that are in the CodeExamples project. Choose codeexamples.example1 and click Select Main Class. Click OK on the Project Properties dialog.

要更改正在使用的主类,请转到“ 文件”菜单,然后选择“ 项目属性” 。 此对话框提供了可以在NetBeans项目中更改的所有选项。 单击运行类别。 在此页面上,有一个Main-Class选项。 当前,它设置为codeexamples.Main (即Main.java类)。 通过单击右侧的“ 浏览”按钮,将出现一个弹出窗口,其中包含CodeExamples项目中的所有主要类。 选择codeexamples.example1 ,然后点击Select Main Class 。 在“ 项目属性”对话框上,单击“ 确定 ”。

Compile and run the application again. The output will now be "four" because the main class being used is now example1.java.

编译并再次运行该应用程序。 现在的输出为“四个”,因为正在使用的主类现在为example1.java

Using this approach it's easy to try out lots of different Java code examples and keep them all in one NetBeans project. but still be able to compile and run them independent of each other.

使用这种方法,可以很容易地尝试许多不同的Java代码示例,并将它们全部保留在一个NetBeans项目中。 但仍然能够相互独立地编译和运行它们。

翻译自: https://www.thoughtco.com/using-multiple-main-classes-2034250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值