gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:29:12 26 4
gpt4 key购买 nike

这种新的界面模型是如何工作的以及是什么

  • 由此引发的菱形继承(钻石问题)
  • 此实现的多重继承特性
  • 以及使用接口(interface)实现的优先级?

最佳答案

Java Lambda FAQ 上有完美的解释.
这是来自 What about the diamond problem? 的引文那里的文章:

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 m inherited by D is unambiguously that defined by A — there is no other possibility. If the situation is changed so that B now also declares a default implementation of m, that becomes the implementation that D inherits by the “most specific implementation” rule. But if both B and C provide default implementations, then they conflict, and D must either use the syntax X.super.m(...) to explicitly choose one of them, or else redeclare the method itself, overriding all supertype declarations.

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

关于java - Java 8 的新默认接口(interface)模型如何工作(包括菱形、多重继承和优先级)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16764791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com