Java接口扩展了java.util包中的接口 [英] Java interface extends interface in the java.util package

查看:126
本文介绍了Java接口扩展了java.util包中的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,接口可以扩展Java中的接口。我有一个问题,如果接口B扩展接口A,B不需要实现A中定义的方法。但是在java.util包中,List接口扩展了Collection接口,并且它实现了Collection方法,这些方法也是只需要方法声明。

As we know, interfaces can extend interface in the Java. I have a question about this, if interface B extends interface A, B need not implement the methods defined in the A. But in the java.util package, the List interface extends Collection interface, and it implements the Collection method, these methods also just have the method declaration.

为什么会这样做,有没有更好的做法?在子接口中实现方法之间是否有任何区别?

Why does it do this, and it there a better practice? Does it make any difference between implementing the method in the sub interface or not?

推荐答案

除了提供/替换方法之外,覆盖方法方法实现,允许提供更具体的javadoc,并缩小返回类型。

Overriding a method, besides providing/replacing a method implementation, allows to provide a more specific javadoc, and to narrow the return type.

例如, Collection.iterator()由下式指定:


返回此集合中元素的迭代器。关于元素返回的顺序没有
保证
(除非这个集合是某个提供
保证的类的实例)。

Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

List.iterator()


以适当的顺序返回此列表中元素的迭代器。

Returns an iterator over the elements in this list in proper sequence.

这篇关于Java接口扩展了java.util包中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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