java为什么集合接口中的方法再次在列表接口中声明,尽管列表扩展了集合
我检查了java Collection
接口和List
接口的源代码。Collection
接口中的一些方法,例如int size()
,在List
接口中重新声明,尽管它不必作为List extends the Collection
接口。(Java 11)
你可以在下面搜索框中键入要查询的问题!
我检查了java Collection
接口和List
接口的源代码。Collection
接口中的一些方法,例如int size()
,在List
接口中重新声明,尽管它不必作为List extends the Collection
接口。(Java 11)
# 1 楼答案
子接口中的方法可能与具有完全相同签名的超级接口中的方法具有不同的行为(或至少更具体的行为)
因此,例如,
List
接口的用户知道add
是非常有用的这比
Collection
接口中add
的一般描述更具体:由于Java8,在子接口中重新声明方法还有另一个原因,那就是
default
接口实现。您可以看到Spliterator<E> spliterator()
在List
、Collection
&Iterable
,并且在每个接口中都有不同的默认实现我同意您在
List
中重新定义size()
的例子没有什么意义,因为Javadoc实际上与Collection
中的size()
相同