🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Java 8 接口更改 - 默认方法和静态方法 > 原文: [https://beginnersbook.com/2017/10/java-8-interface-changes-default-method-and-static-method/](https://beginnersbook.com/2017/10/java-8-interface-changes-default-method-and-static-method/) 在 java 8 之前,java 中的[接口只能有抽象方法。接口的所有方法都是公共的和默认是抽象的。 Java 8 允许接口具有默认和静态方法。我们在接口中使用默认方法的原因是允许开发人员向接口添加新方法,而不会影响实现这些接口的类。](https://beginnersbook.com/2013/05/java-interface/) ## 为什么默认方法? 例如,如果`A`,`B`,`C`和`D`等几个类实现了接口`XYZInterface`,那么如果我们向`XYZInterface`添加新方法,我们必须更改所有代码实现此接口的类(`A`,`B`,`C`和`D`)。在这个例子中,我们只有四个实现我们想要改变的接口的类,但想象如果有数百个类实现一个接口,那么几乎不可能改变所有这些类中的代码。这就是为什么在 java 8 中,我们有一个新概念“默认方法”。这些方法可以添加到任何现有接口,我们不需要在实现类中强制实现这些方法,因此我们可以将这些默认方法添加到现有接口而不会破坏代码。 > 我们可以说在 java 8 中引入了默认方法的概念,以这种方式在现有接口中添加新方法,使它们向后兼容。向后兼容性是在不破坏旧代码的情况下添加新功能。 **接口中的静态方法**类似于默认方法,除了我们不能在实现这些接口的类中覆盖这些方法。 ## Java 8 示例:接口中的默认方法 `MyInterface`中的方法`newMethod()`是一个默认方法,这意味着我们不需要在实现类`Example`中实现这个方法。这样我们就可以将默认方法添加到现有接口,而无需担心实现这些接口的类。 ```java interface MyInterface{ /* This is a default method so we need not * to implement this method in the implementation * classes */ default void newMethod(){ System.out.println("Newly added default method"); } /* Already existing public and abstract method * We must need to implement this method in * implementation classes. */ void existingMethod(String str); } public class Example implements MyInterface{ // implementing abstract method public void existingMethod(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); //calling the abstract method of interface obj.existingMethod("Java 8 is easy to learn"); } } ``` 输出: ```java Newly added default method String is: Java 8 is easy to learn ``` ## Java 8 示例:接口中的静态方法 如上所述,接口中的静态方法与默认方法类似,因此我们不需要在实现类中实现它们。我们可以安全地将它们添加到现有接口,而无需更改实现类中的代码。由于这些方法是静态的,我们不能在实现类中覆盖它们。 ```java interface MyInterface{ /* This is a default method so we need not * to implement this method in the implementation * classes */ default void newMethod(){ System.out.println("Newly added default method"); } /* This is a static method. Static method in interface is * similar to default method except that we cannot override * them in the implementation classes. * Similar to default methods, we need to implement these methods * in implementation classes so we can safely add them to the * existing interfaces. */ static void anotherNewMethod(){ System.out.println("Newly added static method"); } /* Already existing public and abstract method * We must need to implement this method in * implementation classes. */ void existingMethod(String str); } public class Example implements MyInterface{ // implementing abstract method public void existingMethod(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); //calling the static method of interface MyInterface.anotherNewMethod(); //calling the abstract method of interface obj.existingMethod("Java 8 is easy to learn"); } } ``` 输出: ```java Newly added default method Newly added static method String is: Java 8 is easy to learn ``` ## Java 8 - 抽象类与接口 通过在接口中引入默认方法,似乎[抽象类](https://beginnersbook.com/2013/05/java-abstract-class-method/)与 java 8 中的接口相同。然而,这并非完全正确,即使我们现在可以使用具体方法(带有 body 的方法)接口就像抽象类一样,这并不意味着它们是相同的。它们之间仍然存在很少的差异,其中之一是抽象类可以有构造函数,而在接口中我们不能有构造函数。 接口的目的是提供完全抽象,而抽象类的目的是提供部分抽象。这仍然适用。接口就像是您类的蓝图,通过引入默认方法,您可以简单地说我们可以在接口中添加其他功能而不会影响最终用户类。 ## 默认方法和多重继承 当我们有两个具有相同签名的默认方法的接口时,可能会发生[多重继承](https://beginnersbook.com/2013/05/java-multiple-inheritance/)问题。让我们举个例子。 ```java interface MyInterface{ default void newMethod(){ System.out.println("Newly added default method"); } void existingMethod(String str); } interface MyInterface2{ default void newMethod(){ System.out.println("Newly added default method"); } void disp(String str); } public class Example implements MyInterface, MyInterface2{ // implementing abstract methods public void existingMethod(String str){ System.out.println("String is: "+str); } public void disp(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); } } ``` 输出: ```java Error: Duplicate default methods named newMethod with the parameters () and () are inherited from the types MyInterface2 and MyInterface ``` 这是因为我们在接口和编译器中都有相同的方法,不知道要调用哪个方法。 **如何解决这个问题?**为了解决这个问题,我们可以在实现类中实现这个方法,如下所示: ```java interface MyInterface{ default void newMethod(){ System.out.println("Newly added default method"); } void existingMethod(String str); } interface MyInterface2{ default void newMethod(){ System.out.println("Newly added default method"); } void disp(String str); } public class Example implements MyInterface, MyInterface2{ // implementing abstract methods public void existingMethod(String str){ System.out.println("String is: "+str); } public void disp(String str){ System.out.println("String is: "+str); } //Implementation of duplicate default method public void newMethod(){ System.out.println("Implementation of default method"); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); } } ``` 输出: ```java Implementation of default method ```