我和我的一个朋友讨论过在Java中正确使用main方法的问题.基本上我们有这样一个类:
public class AnImporter implements Runnable {
// some methods, attributes, etc.
}
Run Code Online (Sandbox Code Playgroud)
但在哪里放主要方法?我认为将代码保留在其所属的位置是一种很好的做法,从而将上述代码转换为
public class AnImporter implements Runnable {
public static void main(String [] args){
// Startup code for Importer App here
}
// some methods, attributes, etc.
}
Run Code Online (Sandbox Code Playgroud)
虽然我的好友认为"启动代码与应用程序本身无关",但它应该放在另一个类中,如下所示:
public class AnImporter implements Runnable {
// some methods, attributes, etc.
}
public class AnApplication {
// Nothing here
public static void main(String [] args){
AnImporter a = new AnImporter();
// Startup code here
}
// Nothing here
} …Run Code Online (Sandbox Code Playgroud)