gpt4 book ai didi

inheritance - Java 8——默认方法与抽象类的接口(interface)

转载 作者:行者123 更新时间:2023-12-04 03:13:29 31 4
gpt4 key购买 nike

我试图提出一个完整的答案:

"why/when use an abstract class rather than an interface."



并寻找以下方面的验证/建议。

对此的回答是,

"to provide the implementation for some of it. Before the concrete classes come in to define the specific types, an abstract class, typically right below an interface in the inheritance hierarchy (as in many of its examples in the Java APIs) implements and pins down some common aspects of the structure that interface defines.

Another good reason to use an abstract class is that there is a clear logical hierarchy among the types. Abstract class has the use to organize that hierarchy while forcing, by being an abstract class rather than a concrete one, the instantiation of objects only on the concrete classes down below in this hierarchy where they are fully defined and thus are meaningful."



然后,在这种情况下,出现了 Q:

"since Java 8, interfaces can define default method implementations. Why wouldn't i write default method(s) in the interface instead of an abstract class that implements those method(s)? "



答案是:

" One reason is: interface methods are all public, field members are all constants (final & public). You may wanna restrict access privileges of methods and/or make them operate on non-constant state.

Another is: a descendant class can call upon the abstract class method(s) by super, while it can not do so on default interface methods. Also, the interface has no constructors to be invoked by the descendants.

The rest of the reasons are the same as those in pre-Java 8 above. "



除了上述这些,为什么我宁愿选择抽象类而不是接口(interface),尤其是现在我拥有自 Java 8 以来的默认接口(interface)方法?

请注意:我看过 Java 8 default methods vs. non-abstract methods in abstract classesInterface with default methods vs Abstract class in Java 8以及其他一些有用的讨论。这个 Q 是关于 为什么选择抽象类而不是接口(interface)比反过来。

TIA。

最佳答案

以下是选择抽象类而不是接口(interface)的一些原因。您列出的那些 - 私有(private)领域等,是一些主要的。这些是一些更微妙的

  • 类型清晰。你只能扩展一个类。这使您更清楚您的对象是什么以及如何使用它。
  • 菱形继承(钻石问题)。您必须在接口(interface)中实现所有默认方法,以帮助避免菱形问题。如果接口(interface)是 Collections,这可能是一个巨大的痛苦,因为它们有十亿个。使用抽象类,您只需覆盖
  • 所需的内容
  • 在 Java 8 中,有 lambda 表达式,并且使用要实现的方法传递接口(interface)的旧例程已被篡改。尽管如此,当你看到一个带有默认方法的接口(interface)时,这可能会以你可能不想要的方式解释

  • 以下是甲骨文对此主题的看法:

    Which should you use, abstract classes or interfaces? Consider using abstract classes if any of these statements apply to your situation:

    • You want to share code among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.


    在这篇文章中,Orace 为这两种类型系统之间的区别辩护
    https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

    编辑 澄清“菱形继承(钻石问题)”项目符号
    问题描述here(和许多其他地方)
    http://www.lambdafaq.org/what-about-the-diamond-problem/

    当您从两个声明相同方法的地方继承时会出现问题,并且在解析函数调用时必须选择一个。这在 Java 7 中从来都不是问题,因为您只能扩展一个类并且接口(interface)没有方法。

    现在的解决策略有点复杂,但这里有一个很好的描述:
    (来自 http://blog.loxal.net/2013/05/java-8-default-interface.html)

    To address the diamond problem there is a precedence in which order an implementation is used: only if the class implements all default / optional methods of its interfaces, the code can be compiled and the implementations of this class are used. Otherwise the 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. Also if the class implements an interface's default method, the implementation of the class will be used instead of interfaces's default implementation.



    如果你坚持抽象类,你永远不会遇到这个问题。但是,如果您的对象需要实现两个接口(interface)(例如,因为它需要添加到期望这些类型的列表中), 这些接口(interface)有冲突的方法签名,你最终将不得不重新定义一大堆方法,即使这意味着你只是在进行 super 调用,这有点违背了基于继承的动态调度的要点。这不是一个交易破坏者,而是在构建一个复杂的 Java 项目时需要考虑的事情

    关于inheritance - Java 8——默认方法与抽象类的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26895604/

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