Java 8 的新默认接口模型如何工作(包括菱形、多重继承和优先级)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16764791/
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-10-31 23:56:26  来源:igfitidea点击:

How does Java 8' new default interface model works (incl. diamond, multiple inheritance, and precedence)?

javajava-8

提问by Alex

How does this new interface model works and what is about

这个新的界面模型是如何工作的,是关于什么的

  • the diamond problem that might arise out of this
  • multiple inheritance character of this implementation
  • and the precedence with which the interface implementations are used ?
  • 由此可能产生的钻石问题
  • 此实现的多重继承特性
  • 以及使用接口实现的优先级?

采纳答案by Alex

Here is a detailed explanation for Java 8' new interface model & the diamond problem of multiple inheritance.

这里详细解释了Java 8的新接口模型和多重继承的菱形问题

As you might see in this examples, starting with JDK 8, Java has introduced a kind of multiple inheritanceas both, the classand its interfacemight contain an implementationof the same method (same name & signature). To address the diamond problem there is a precedence in which order an implementation is used: only if the class implements alldefault / optional methods of its interfaces, the code can be compiled and the implementations of this class are used. Otherwisethe compiler tries to patch the missing implementation(s) with interface's default implementation. And if there are multiple default implementations of a method, then the diamond problem occurs and the compiler rejects the compilation.
Java 8' new interfaces model is the result of approaching backwards compatibility, i. e. to keep existing code that was written against pre Java 8 interfaces compilable.

正如你可能在这个例子中看到,从JDK 8,Java已经推出了一种多重继承作为两者的 和它的接口可能包含 执行同样的方法(相同的名称和签名)的。为了解决菱形问题,使用实现的顺序有一个优先级:只有当类实现其接口的所有默认/可选方法时,才能编译代码并使用此类的实现。否则编译器会尝试使用接口的默认实现来修补缺失的实现. 如果一个方法有多个默认实现,就会出现菱形问题,编译器拒绝编译。
Java 8 的新接口模型是接近向后兼容性的结果 ,即保持针对 Java 8 之前的接口编写的现有代码可编译。

回答by Anna Zubenko

There is a perfect explanation at Java Lambda FAQ.
Here is a citation from What about the diamond problem?article there:

Java Lambda FAQ 中有一个完美的解释。
这是钻石问题怎么样?那里的文章:

interface A {
    default void m() { ... }        
}
interface B extends A {}
interface C extends A {}
class D implements B, C {}

In the initial case (the code above), the implementation of minherited by Dis unambiguously that defined by A— there is no other possibility. If the situation is changed so that Bnow also declares a default implementation of m, that becomes the implementation that Dinherits by the “most specific implementation” rule. But if both Band Cprovide default implementations, then they conflict, and Dmust either use the syntax X.super.m(...)to explicitly choose one of them, or else redeclare the method itself, overriding all supertype declarations.

在最初的情况下(上面的代码),由D继承的m的实现是明确的由A定义的——没有其他可能性。如果情况发生变化,那么B现在也声明了m的默认实现,这将成为D通过“最具体实现”规则继承的实现。但是如果BC 都提供默认实现,那么它们就会发生冲突,并且D必须使用语法X.super.m(...)来显式选择其中之一,或者重新声明方法本身,覆盖所有超类型声明。

Be sure to check out previous article on rules of resolving conflicting method declarations and other articles on Java Lambda project — they are quite good.

请务必查看之前关于解决冲突方法声明的规则的文章以及有关 Java Lambda 项目的其他文章——它们都非常好。