为什么使用接口默认方法? [英] why Interface Default methods?

查看:126
本文介绍了为什么使用接口默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Java 8的默认方法. 与其他任何资源一样,此链接在互联网上说

Learning java 8 default methods . This link like any other resource on internet says

在最严格的意义上",默认方法是倒退的,因为 它们允许您使用代码污染"您的界面.但是他们 提供最优雅,最实用的方法来允许倒退 兼容性. Oracle可以轻松地更新所有 集合类,并为您改进现有代码以用于 Lambda.

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开发者/设计者在接口中提供了默认方法,因此所有实现类都不必不必要地 覆盖相同的行为,因此提供向后兼容性.例如:-如果ForEach方法不是默认方法,则每个实现类的集合都必须实现它.同意.

My understanding is that java 8 dev/designers provided the default method in interfaces so that all implementing class does not have to unnecessarily override same behavior, hence provide backward compatibility. For example :- if ForEach method would not have been default method, every collection implementing class had to implement it. Agreed .

要克服这一点,我们可以让一个类提供这些默认方法的实现,然后实现诸如arraylist等的类 已经扩展了这一点.这样,我们就可以同时规范Java基础知识(即可重用性和抽象性),即保持接口污染少

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. This way we could have statisfy both java fundamentals i.e reusability and abstraction i.e keeping the interface pollution less

我确信Java 8开发人员/设计师已经对此进行了思考,因为他们的知识更多,并且我在这里遗漏了一些东西.有人可以在这里提供帮助,以便我们开发人员也可以在此重大变化中脱颖而出吗?

I am sure java 8 dev/designer have already thought about this as they are much more learned and i am missing something here. Can someone help here so that we developers can also be on top of it as this major change?

推荐答案

要克服这一点,我们可以让一个类提供这些默认方法的实现,然后实现诸如arraylist等的类可以扩展该类.

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类(因为它们通常会扩展某些基本类,例如AbstractCollectionAbstractList,因此可以添加新方法的实现).

Your suggestion would work only for standard JDK classes (since they usually extends some base classes such as AbstractCollection and AbstractList, were the implementation of the new methods can be added).

实现JDK接口的自定义类呢?例如,如果您有一个实现List的类,但是没有扩展某些JDK List实现,则应该能够切换到Java 8,而不必在类中实现新的方法.

What about custom classes that implement JDK interfaces? If, for example, you have a class that implements List but doesn't extend some JDK List implementation, you should be able to switch to Java 8 without having to implement new methods in your class.

使用List界面中新方法的default实现,您不必触摸自定义类.如果您对默认实现不满意,则可以稍后向这些方法添加自定义实现.

With default implementations of new methods in the List interface, you don't have to touch your custom class. You can later add a custom implementation to those methods if you are not satisfied by the default implementation.

这篇关于为什么使用接口默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆