[英]Need for default method implementation in Java 8
While going through the answers for "Need of the defender methods" I came across this answer在浏览“防御者方法的需要”的答案时,我遇到了这个答案
Can you please elaborate what does the below mean in the accepted answer?您能否详细说明以下在接受的答案中的含义?
you should be able to switch to Java 8 without having to implement new methods in your class.
您应该能够切换到 Java 8,而无需在您的类中实现新方法。
The asker of the original question, M Sach, said that instead of default methods, just put the default implementations in an abstract class.原始问题的提问者 M Sach 说,不要使用默认方法,只需将默认实现放在抽象类中即可。
Eran, the answerer, says that this would break existing code.回答者 Eran 说这会破坏现有的代码。
For example, suppose you have your own List
implementation written in Java 7, which does not inherit from AbstractList
.例如,假设您有自己的
List
实现,它是用 Java 7 编写的,它不是从AbstractList
继承的。 When you migrate your code to Java 8, what happens?当您将代码迁移到 Java 8 时,会发生什么? There is suddenly a bunch of new methods in the
List
interface that your own List
implementation does not have (the "default" implementation of those methods are in AbstractList
, which your class doesn't inherit)! List
接口中突然出现了一堆您自己的List
实现没有的新方法(这些方法的“默认”实现在AbstractList
,您的类没有继承它)! Your code will no longer compile.您的代码将不再编译。
This means that when migrating to Java 8, you'd potentially have to write new methods for existing classes.这意味着在迁移到 Java 8 时,您可能必须为现有类编写新方法。
With default methods however, the default implementation is in the interface itself, so you don't have to write new methods.但是,对于默认方法,默认实现在接口本身中,因此您不必编写新方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.