gpt4 book ai didi

java - Java是否有计划用默认方法(java8)替代抽象类?

转载 作者:行者123 更新时间:2023-12-02 05:09:52 32 4
gpt4 key购买 nike

Java是否有计划用默认方法替代抽象类?我找不到使用默认方法而不是 Abstract 的真实案例?

最佳答案

不存在这样的计划,您可以通过比较已记录的意图得出这些计划,这与此类计划的含义不同:

Stuart Marks writes:

The main goal is to allow interface evolution, that is, the addition of new methods. If a new method is added to an interface, existing classes that implement the interface would be missing an implementation, which would be incompatible. To be compatible, an implementation has to come from somewhere, so it is provided by default methods.

The main intent of a Java interface is to specify a contract that any class can implement without having to alter its position in the class hierarchy. It's true that, prior to Java 8, interfaces were purely abstract. However, this is not an essential property of interfaces. Even when default methods are included, an interface at its heart still specifies a contract upon the implementing class. The implementing class can override default methods, so the class is still in complete control of its implementation. (Note also that default methods cannot be final.)

Brian Goetz writes:

The proximate reason for adding default methods to interfaces was to support interface evolution, …

Here are some use cases that are well within the design goals:

  • Interface evolution. Here, we are adding a new method to an existing interface, which has a sensible default implementation in terms of existing methods on that interface. An example would be adding the forEach method to Collection, where the default implementation is written in terms of the iterator() method.

  • "Optional" methods. Here, the designer of an interface is saying "Implementors need not implement this method if they are willing to live with the limitations in functionality that entails". For example, Iterator.remove was given a default which throws UnsupportedOperationException; since the vast majority of implementations of Iterator have this behavior anyway, the default makes this method essentially optional. (If the behavior from AbstractCollection were expressed as defaults on Collection, we might do the same for the mutative methods.)

  • Convenience methods. These are methods that are strictly for convenience, again generally implemented in terms of non-default methods on the class. The logger() method in your first example is a reasonable illustration of this.

  • Combinators. These are compositional methods that instantiate new instances of the interface based on the current instance. For example, the methods Predicate.and() or Comparator.thenComparing() are examples of combinators.

请注意,这些并不针对抽象类的主要领域,例如提供框架实现。除了 technical differences 之外,抽象类在语义上是不同的,因为它们承担有关如何实现功能的设计决策,而接口(interface),即使使用默认方法,也不应该这样做。例如。一个著名的例子是 List 接口(interface),该接口(interface)存在两个根本不同的抽象类:AbstractListAbstractSequentialList 以及子类的选择实现完全不同的List 不应该被接口(interface)取消。因此 List 接口(interface)定义了契约,并且永远不能替代提供特定基本实现的抽象类。

关于java - Java是否有计划用默认方法(java8)替代抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39913667/

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