Java多个类多个main方法,执行所有main方法
Java multiple Classes and multiple main methods, execute all main methods
我是 Java 的新手,我刚刚写了一些代码,其中我使用了两个 类 和主要方法。
我喜欢一个接一个地执行这两个主要方法。是否可以按指定顺序同时执行它们?
imFirst.java
public class imFirst {
public static void main(String[] args) {
System.out.println("I want to be the first one executed!");
}
}
imSecond.java
public class imSecond {
public static void main(String[] args) {
System.out.println("I want to be the second one executed!");
}
}
这些在一个包中,通过 eclipse 执行。
你可以从imFirst调用imSecond的main:
public class imFirst {
public static void main(String[] args) {
System.out.println("I want to be the first one executed!");
imSecond.main(args);
}
}
或者可以相反:
public class imSecond {
public static void main(String[] args) {
System.out.println("I want to be the second one executed!");
imFirst.main(args);
}
}
根据您的需要进行操作。但是不要同时做这两件事,否则你会得到两种方法相互调用的无限循环。
附带说明:使用正确的 java 命名约定。 Class 名字应该是 CamelCase。
快速修复
您也可以像调用其他所有常规方法一样调用 main
方法:
public static void main(String[] args) {
imFirst.main(null);
imSecond.main(null);
}
更好的方法
但是您应该首先考虑为什么您甚至需要 两个主要方法。 main
方法是整个 Java 链中的第一件事,通常您只对每个完整程序使用 one。目的是简单地启动程序,大多数时候它只是调用专用的 class,例如:
public static void main(String[] args) {
ProgramXY programXY = new ProgramXY();
programXY.init();
programXY.start();
}
所以我建议您将两个打印语句简单地移动到自己的 classes 和方法中,然后简单地从 一个主要方法 :
中调用它们
实用程序class:
public class ConsolePrinter {
public static void println(String line) {
System.out.println(line);
}
}
唯一的主方法:
public static void main(String[] args) {
ConsolePrinter.println("I want to be the first one executed!");
ConsolePrinter.println("I want to be the second one executed!");
}
更一般
或更一般的目的:
第一个class:
public class FirstClass {
public void firstMethod() {
// ...
}
}
第二个class:
public class SecondClass {
public void secondMethod() {
// ...
}
}
唯一的主要方法:
public static void main(String[] args) {
FirstClass first = new FirstClass();
SecondClass second = new SecondClass();
first.firstMethod();
second.secondMethod();
}
我是 Java 的新手,我刚刚写了一些代码,其中我使用了两个 类 和主要方法。 我喜欢一个接一个地执行这两个主要方法。是否可以按指定顺序同时执行它们?
imFirst.java
public class imFirst {
public static void main(String[] args) {
System.out.println("I want to be the first one executed!");
}
}
imSecond.java
public class imSecond {
public static void main(String[] args) {
System.out.println("I want to be the second one executed!");
}
}
这些在一个包中,通过 eclipse 执行。
你可以从imFirst调用imSecond的main:
public class imFirst {
public static void main(String[] args) {
System.out.println("I want to be the first one executed!");
imSecond.main(args);
}
}
或者可以相反:
public class imSecond {
public static void main(String[] args) {
System.out.println("I want to be the second one executed!");
imFirst.main(args);
}
}
根据您的需要进行操作。但是不要同时做这两件事,否则你会得到两种方法相互调用的无限循环。
附带说明:使用正确的 java 命名约定。 Class 名字应该是 CamelCase。
快速修复
您也可以像调用其他所有常规方法一样调用 main
方法:
public static void main(String[] args) {
imFirst.main(null);
imSecond.main(null);
}
更好的方法
但是您应该首先考虑为什么您甚至需要 两个主要方法。 main
方法是整个 Java 链中的第一件事,通常您只对每个完整程序使用 one。目的是简单地启动程序,大多数时候它只是调用专用的 class,例如:
public static void main(String[] args) {
ProgramXY programXY = new ProgramXY();
programXY.init();
programXY.start();
}
所以我建议您将两个打印语句简单地移动到自己的 classes 和方法中,然后简单地从 一个主要方法 :
中调用它们实用程序class:
public class ConsolePrinter {
public static void println(String line) {
System.out.println(line);
}
}
唯一的主方法:
public static void main(String[] args) {
ConsolePrinter.println("I want to be the first one executed!");
ConsolePrinter.println("I want to be the second one executed!");
}
更一般
或更一般的目的:
第一个class:
public class FirstClass {
public void firstMethod() {
// ...
}
}
第二个class:
public class SecondClass {
public void secondMethod() {
// ...
}
}
唯一的主要方法:
public static void main(String[] args) {
FirstClass first = new FirstClass();
SecondClass second = new SecondClass();
first.firstMethod();
second.secondMethod();
}