Java Main 方法,良好的编码风格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/732151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:49:45  来源:igfitidea点击:

Java Main Method, Good Coding Style

javacoding-style

提问by kryoko

I've had quite a long discussion with a friend of mine about the correct and good use of the main method in Java. Basically we have a class like this:

我和我的一个朋友就 Java 中 main 方法的正确和良好使用进行了很长时间的讨论。基本上我们有一个这样的类:

public class AnImporter implements Runnable {
  // some methods, attributes, etc.
}

But where to put the main method? I concider it a good practice to "keep code where it belongs", thus turning the above code into

但是把main方法放在哪里呢?我认为“将代码保留在它所属的地方”是一个很好的做法,从而将上面的代码变成

public class AnImporter implements Runnable {
  public static void main(String [] args){
    // Startup code for Importer App here
  }
  // some methods, attributes, etc.
}

While my buddy argues that "the startup code has nothing to do with the application itself", thus it should be placed in another class, like this:

虽然我的朋友认为“启动代码与应用程序本身无关”,因此它应该放在另一个类中,如下所示:

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
}

Despite the fact that we discussed the matter for some time we both ended up with no conclusion on which way is the better approach in Java. What's your oppinion on this topic? Where and most importantly, why, do you place your main method where you placed it?

尽管我们讨论了一段时间,但最终我们都没有得出结论,哪种方式是 Java 中更好的方法。你对这个话题有什么看法?最重要的是,为什么将主要方法放在放置它的地方?

采纳答案by Clint Miller

I agree with your friend. You're building up a potentially reusable service in AnImporter that could potentially be used in multiple programs with multiple main's. So, making one main special and embedding it in AnImporter doesn't make much sense.

我同意你朋友的看法。您正在 AnImporter 中构建一个潜在的可重用服务,该服务可能会在具有多个 main 的多个程序中使用。因此,制作一个特殊的主并将其嵌入到 AnImporter 中并没有多大意义。

回答by cletus

I wouldn't pollute a Runnable class with a main method. The same goes for pretty much any class that does anything in your application. Generally I'll have a class like this:

我不会用 main 方法污染 Runnable 类。几乎所有在您的应用程序中执行任何操作的类也是如此。通常我会有这样的课程:

public class App {
  public static void main(String args[]) {
    Thread t = new Thread(new Blah());
    t.start();
      synchronized (t) {
        t.wait();
      }
  }
}

public class Blah implements Runnable {
  public void run() {
    // do normal stuff
  }
}

instead of:

代替:

public class Blah implements Runnable {
  public void run() {
    // do normal stuff
  }

  public static void main(String args[]) {
    Thread t = new Thread(new Blah());
    t.start();
    synchronized (t) {
      t.wait();
    }
  }
}

It just feels cleaner.

只是感觉更干净。

回答by digiarnie

I'd probably go with your friend as I'd prefer to get out of the class with the main method as quickly as possible. It helps facilitate testing when you want to test atomically (just the runnable class) or you want to mock things out. The sooner you get out of the main method, the more options you have. If you have one class with the main method and other things in it, it could get messy quickly. (even if it might not seem that way with a simple example such as the one you describe)

我可能会和你的朋友一起去,因为我更喜欢用 main 方法尽快离开课堂。当您想要以原子方式进行测试(只是可运行的类)或想要模拟时,它有助于促进测试。您越早退出 main 方法,您拥有的选择就越多。如果你有一个包含 main 方法和其他东西的类,它可能很快就会变得混乱。(即使使用您描述的简单示例看起来可能并非如此)

But I'd say readability and testability are two good reasons for getting out of the main method (and its encompassing class) ASAP. But hey..that's just me ;)

但我想说可读性和可测试性是尽快退出主要方法(及其包含的类)的两个很好的理由。但是嘿..那只是我;)

回答by Uri

I always separate the main from the rest of the code, for several reasons:

我总是将主要代码与其余代码分开,原因如下:

1) A main is, in a way, a hack to let your program start from the command line. Any class that contains it should have a single responsibility: let the program start from the command line. By putting it with your primary runnable, you're polluting the runnable.

1) 从某种意义上说,main 是一种让您的程序从命令行启动的技巧。任何包含它的类都应该有一个责任:让程序从命令行启动。把它和你的主要 runnable 放在一起,你正在污染 runnable。

2) You could end up having multiple mains (e.g., with certain default parameters, with special modes, etc.)

2) 您可能最终拥有多个电源(例如,使用某些默认参数、使用特殊模式等)

3) You could end up running the program from a different environment (e.g., an Eclipse plugin or OGSI module, an applet, a web based tool, etc.). In those cases, you would want to restrict access to your main. Putting it with the functionality prevents that.

3) 您最终可能会从不同的环境(例如,Eclipse 插件或 OGSI 模块、小程序、基于 Web 的工具等)运行程序。在这些情况下,您可能希望限制对 main.js 的访问。将它与功能放在一起可以防止这种情况。

4) It is sometimes easier to leave your main in the default package to make run time execution faster (e.g., java myblabla par1 par2 par3), but you definitely don't want the rest of your code in the default package.

4) 有时将 main 保留在默认包中更容易,以便更快地执行运行时(例如,java myblabla par1 par2 par3),但您绝对不希望默认包中的其余代码。

回答by S.Lott

The interface to main (a list of strings) is approximately useless, except for the OS shell.

除了 OS shell 之外,main(字符串列表)的接口几乎没用。

Your main should have as little code as humanly possible in it.

您的 main 应该包含尽可能少的代码。

Indeed, your public class ThisIsMyApp {...}should be nothing more than the OS interface to the real work, which is elsewhere.

确实,您public class ThisIsMyApp {...}应该无非是真正工作的 OS 接口,这在别处。

回答by kisp

I would separate the main method from the code.

我会将主要方法与代码分开。

Although I also have a different type of project. It includes not a real working program for a solution. Here I need to run different solutions for different problems using (and developing) the same library. Different problems are not parallel. I need to run one single problem alone from my IDE. I found it convenient to use the same project with a huge number of classes with PSVM methods.

虽然我也有不同类型的项目。它不包括用于解决方案的实际工作程序。在这里,我需要使用(和开发)相同的库针对不同的问题运行不同的解决方案。不同的问题不是平行的。我需要从我的 IDE 中单独运行一个问题。我发现通过 PSVM 方法使用具有大量类的同一个项目很方便。

This project contains programming contests solutions for over 400 of different problems. Do you have a better organizing for this?

该项目包含针对 400 多个不同问题的编程竞赛解决方案。你有更好的组织方式吗?