为什么接口默认方法?
why Interface Default methods?
正在学习java 8 个默认方法。
这 link 就像互联网上的任何其他资源一样
In ‘the strictest sense’, Default methods are a step backwards because
they allow you to ‘pollute’ your interfaces with code. But they
provide the most elegant and practical way to allow backwards
compatibility. It made it much easier for Oracle to update all the
Collections classes and for you to retrofit your existing code for
Lambda.
我的理解是 java 8 dev/designers 在接口中提供了默认方法,因此所有实现 class 都不必不必要地
覆盖相同的行为,从而提供向后兼容性。例如:- 如果 ForEach 方法不是默认方法,则每个实现 class 的集合都必须实现它。同意。
为了克服这个问题,我们可以有一个 class 提供这些默认方法的实现,然后像 arraylist 等那样实现 class
已经扩展了。这样我们就可以同时统计 java 基础知识,即可重用性和抽象性,即减少接口污染
我相信 java 8 dev/designer 已经考虑过这个问题,因为他们学识渊博,我在这里遗漏了一些东西。有人可以在这里提供帮助,以便我们开发人员也可以在这一重大变化中处于领先地位吗?
To overcome that we could have had one class providing implementation of these default methods and then implementing class like arraylist etc could have extended that.
您的建议仅适用于标准 JDK classes(因为它们通常会扩展一些基础 classes,例如 AbstractCollection
和 AbstractList
,可以添加新方法的实现)。
实现 JDK 接口的自定义 class 怎么样?例如,如果你有一个 class 实现了 List
但没有扩展一些 JDK List
实现,你应该能够切换到 Java 8 无需在 class.
中实施新方法
通过 default
在 List
接口中实现新方法,您不必触及您的自定义 class。如果您对默认实现不满意,可以稍后向这些方法添加自定义实现。
在Java8之前,interfaces
只能有abstract methods
。这些方法的实现必须在单独的 class 中提供。因此,如果要在 interface
中添加新方法,则必须在实现相同接口的 class 中提供其实现代码。
为了克服这个问题,Java 8 引入了 default methods
的概念,它允许接口具有带有实现的方法而不影响实现 [=12] 的 classes =].
引入 default methods
是为了提供向后可比性,以便现有 interfaces
可以使用 lambda 表达式,而无需实现实现 class 中的方法。 Default methods
也称为 defender methods
或 virtual extension methods
。
如果需要向接口添加新方法,使用现有接口的客户端将被破坏,因为类需要实现接口中的所有方法。
在这种情况下,可以使用default and static methods。这些方法可以有主体,客户不需要实现它们,因此现有的实现无需任何更改即可工作。
例如,如果要增强接口以添加接受 lambda 表达式的方法,则可以使用默认方法。
正在学习java 8 个默认方法。 这 link 就像互联网上的任何其他资源一样
In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code. But they provide the most elegant and practical way to allow backwards compatibility. It made it much easier for Oracle to update all the Collections classes and for you to retrofit your existing code for Lambda.
我的理解是 java 8 dev/designers 在接口中提供了默认方法,因此所有实现 class 都不必不必要地 覆盖相同的行为,从而提供向后兼容性。例如:- 如果 ForEach 方法不是默认方法,则每个实现 class 的集合都必须实现它。同意。
为了克服这个问题,我们可以有一个 class 提供这些默认方法的实现,然后像 arraylist 等那样实现 class 已经扩展了。这样我们就可以同时统计 java 基础知识,即可重用性和抽象性,即减少接口污染
我相信 java 8 dev/designer 已经考虑过这个问题,因为他们学识渊博,我在这里遗漏了一些东西。有人可以在这里提供帮助,以便我们开发人员也可以在这一重大变化中处于领先地位吗?
To overcome that we could have had one class providing implementation of these default methods and then implementing class like arraylist etc could have extended that.
您的建议仅适用于标准 JDK classes(因为它们通常会扩展一些基础 classes,例如 AbstractCollection
和 AbstractList
,可以添加新方法的实现)。
实现 JDK 接口的自定义 class 怎么样?例如,如果你有一个 class 实现了 List
但没有扩展一些 JDK List
实现,你应该能够切换到 Java 8 无需在 class.
通过 default
在 List
接口中实现新方法,您不必触及您的自定义 class。如果您对默认实现不满意,可以稍后向这些方法添加自定义实现。
在Java8之前,interfaces
只能有abstract methods
。这些方法的实现必须在单独的 class 中提供。因此,如果要在 interface
中添加新方法,则必须在实现相同接口的 class 中提供其实现代码。
为了克服这个问题,Java 8 引入了 default methods
的概念,它允许接口具有带有实现的方法而不影响实现 [=12] 的 classes =].
引入 default methods
是为了提供向后可比性,以便现有 interfaces
可以使用 lambda 表达式,而无需实现实现 class 中的方法。 Default methods
也称为 defender methods
或 virtual extension methods
。
如果需要向接口添加新方法,使用现有接口的客户端将被破坏,因为类需要实现接口中的所有方法。
在这种情况下,可以使用default and static methods。这些方法可以有主体,客户不需要实现它们,因此现有的实现无需任何更改即可工作。
例如,如果要增强接口以添加接受 lambda 表达式的方法,则可以使用默认方法。