简体   繁体   English

Java 8 接口中的静态方法的目的是什么?

[英]What is the purpose of a static method in interface from Java 8?

Why are static methods supported from Java 8?为什么 Java 8 支持静态方法? What is the difference between the two lines in main method in below code?下面代码中 main 方法中的两行有什么区别?

package sample;
public class A {
    public static void doSomething()
    {
        System.out.println("Make A do something!");
    }
}

public interface I {
    public static void doSomething()
    {
        System.out.println("Make I do something!");
    }
}

public class B {
    public static void main(String[] args) {
        A.doSomething(); //difference between this
        I.doSomething(); //and this
    }
}

As we can see above, I is not even implemented in B. What purpose would it serve to have a static method in an interface when we can write the same static method in another class and call it?正如我们在上面看到的,I 甚至没有在 B 中实现。当我们可以在另一个类中编写相同的静态方法并调用它时,在接口中拥有一个静态方法有什么用? Was it introduced for any other purpose than modularity.它是否出于模块化以外的其他目的而引入。 And by modularity, I mean the following:通过模块化,我的意思是:

public interface Singable {
    public void sing();
    public static String getDefaultScale()
    {
        return "A minor";
    }
}

Just to put like methods together.只是将类似的方法放在一起。

In the past, if you had an interface Foo and wanted to group interface-related utils or factory methods, you would need to create a separate utils class FooUtils and store everything there.过去,如果您有一个接口Foo并且想要对与接口相关的实用程序或工厂方法进行分组,则需要创建一个单独的FooUtilsFooUtils并将所有内容存储在那里。

Those classes would not have anything in common other than the name, and additionally, the utils class would need to be made final and have a private constructor to forbid unwanted usage.除了名称之外,这些类没有任何共同点,此外,utils 类需要成为final并具有私有构造函数以禁止不必要的使用。

Now, thanks to the interface static methods, you can keep everything in one place without creating any additional classes.现在,由于接口静态方法,您可以将所有内容保存在一个地方,而无需创建任何其他类。

It's also important to not forget all good practices and not throw everything mindlessly to one interface class - as pointed out in this answer同样重要的是不要忘记所有好的做法,不要将所有东西都扔到一个接口类中 - 正如这个答案中所指出的

There are mainly two reasons for static method inside interfaces: create instances of those interfaces (and the code is clearly where it has to be);接口内部的静态方法主要有两个原因: create instances这些接口的create instances (并且代码很清楚它必须在哪里); like Predicate::isEqual that would create a Predicate based provided Object;Predicate::isEqual会创建一个基于Predicate的提供对象; or Comparator::comparing , etc. And the second reason would be utility methods that are general per all those types;Comparator::comparing等。第二个原因是所有这些类型通用的utility methods like Stream::ofStream::of

Still an interface has to be clear and does not have to create additional clutter in the API.接口仍然必须清晰,并且不必在 API 中创建额外的混乱。 Even the jdk code has Collectors - static factory methods, but a Collector interface at the same time for example.甚至 jdk 代码也有Collectors - 静态工厂方法,但同时有一个Collector接口,例如。 Those methods could be merged into Collector interface, but that would make the interface more clunky than it has to be.这些方法可以合并到Collector接口中,但这会使接口变得比它必须的更笨拙。

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

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