有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    子接口中的方法可能与具有完全相同签名的超级接口中的方法具有不同的行为(或至少更具体的行为)

    因此,例如,List接口的用户知道add是非常有用的

    Appends the specified element to the end of this list

    这比Collection接口中add的一般描述更具体:

    Ensures that this collection contains the specified element (optional operation)

    由于Java8,在子接口中重新声明方法还有另一个原因,那就是default接口实现。您可以看到Spliterator<E> spliterator()ListCollection&Iterable,并且在每个接口中都有不同的默认实现

    我同意您在List中重新定义size()的例子没有什么意义,因为Javadoc实际上与Collection中的size()相同