简体   繁体   English

为什么 Java 8 中的接口允许有 main 方法?

[英]Why are interfaces in Java 8 allowed to have the main method?

Why are interfaces allowed to have a main method in Java 8?为什么接口允许在 Java 8 中有一个main方法?

As stated in below code it works fine and produces output properly.如下面的代码所述,它工作正常并正确产生输出。

public interface Temp {
    public static void main(String args[]){
         System.out.println("Hello");
    }
}

Currently it is behaving like a class and I have executed interface with main method.目前它的行为就像一个类,我已经用 main 方法执行了interface

Why do we need this?我们为什么需要这个?

Since Java 8, static methods are allowed in interfaces.从 Java 8 开始,接口中允许使用静态方法。

main() is a static method. main() 是一个静态方法。

Hence, main() is allowed in interfaces.因此,接口中允许使用 main()。

We don't need this, since it wasn't allowed before, and yet we survived.我们不需要这个,因为以前是不允许的,但我们还是活了下来。 But since static methods, by definition, are not bound to an instance of a class, but to the class itself, it makes sense to allow them in interfaces.但是由于静态方法,根据定义,不是绑定到类的实例,而是绑定到类本身,所以在接口中允许它们是有意义的。 It allows defining utility methods related to an interface (like the ones found in Collections , for example), in the interface itself, rather than a separate class).它允许在接口本身而不是单独的类中定义与接口相关的实用程序方法(例如在Collections找到的那些)。

There is no difference between class static methods and interface static methods.类静态方法和接口静态方法之间没有区别。

I second the answer of @jb-nizet.我支持@jb-nizet 的回答。 There is no "desparate need" for this, but it removes an unnecessary restriction.对此没有“迫切需要”,但它消除了不必要的限制。 Eg one example is, that you can now declare a factory method within the interface:例如,您现在可以在接口中声明一个工厂方法:

 public interface SomeService {

   public static SomeService getInstance() {
     // e.g. resolve via service provider interface
   }

   ...

 }

Before Java 8 we needed always a separate factory class.在 Java 8 之前,我们总是需要一个单独的工厂类。 One favorite example is the google app engine API.一个最喜欢的例子是谷歌应用引擎 API。

In Java 8 an interface can have static methods.在 Java 8 中,接口可以有静态方法。 Since the main method is also a static method, it will allow it.由于 main 方法也是一个静态方法,它会允许它。

We can declare common helper methods using these static methods.我们可以使用这些静态方法声明常见的辅助方法。

More of an addendum: maybe one thought here is to resemble what you can do with the Application trait in Scala:更多的附录:也许这里的一个想法是类似于您可以使用 Scala 中的Application trait 执行的操作:

object Main extends Application {
  Console.println("Hello World!")
}

simply by extending Application , you turn a class into something that runs.只需通过扩展Application ,您就可以将一个类变成可以运行的东西。

From Brian Goetz's in the comments:来自 Brian Goetz 在评论中的评论:

I think you have the question backwards.我想你的问题倒退了。 The question is not "should main methods be allowed", it is "should we have explicitly disallowed main methods now that static methods are permissible in interfaces" (or, equivalently, exclude interface classes as targets for the java launcher.) This would have been adding extra rules just to exclude something some people perceive as weird (but not dangerous) -- and that's a losing game.问题不是“是否应该允许主方法”,而是“既然接口中允许静态方法,我们是否应该明确禁止主方法”(或者,等效地,将接口类排除为 java 启动器的目标。)这将有添加额外的规则只是为了排除一些人认为奇怪(但不危险)的东西——这是一场失败的游戏。 You want to define as few new rules as you can reasonably get away with -- because otherwise you get mired in complexity.您希望尽可能少地定义新规则,因为否则您会陷入复杂性的泥潭。

Personally I've always found weird the fact that I had to elect a class to be the main class (ie to host the main method), so putting the main method in an interface does not sound weird at all to me. 就我个人而言,我总是觉得很奇怪,因为我不得不选择一个类作为主类(即,承载主方法),因此将main方法放在接口中对我来说一点都不奇怪。 If the main method is in an interface, I don't have to think about "what else to put in the main class" to justify its existence in an OO program. 如果main方法在接口中,则不必考虑“在主类中还有什么”来证明它在OO程序中的存在。 If the main class contains only the main method, it makes it look like a procedural program. 如果主类仅包含main方法,则使其看起来像一个程序程序。 If I put other members in the main class, it doesn't look cohesive at all, so...welcome main method in interfaces! 如果我将其他成员放在主类中,那么它看起来根本没有凝聚力,因此...在接口中欢迎main方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM