gpt4 book ai didi

Java接口(interface)扩展了java.util包中的接口(interface)

转载 作者:行者123 更新时间:2023-12-03 06:13:09 24 4
gpt4 key购买 nike

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

为什么要这样做,是否有更好的做法?在子接口(interface)中实现方法与不实现有什么区别吗?

最佳答案

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

例如,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接口(interface)扩展了java.util包中的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205286/

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