扩展其他接口但也包含相同方法的接口

Jam*_*ith 2 java oop extends interface

只要查看 Java Collections,我就会看到 List 接口,它扩展了 Collection 接口,也包含 Collection 接口中完全相同的方法签名 - 但并非所有这些都只是一个子集。我的问题是为什么?由于它扩展了它,我们知道它默认会继承它们。为什么是子集?

扩展队列的双端队列也可以看到同样的情况,包含队列的所有方法签名。这难道不会让我们更难了解该界面的独特之处吗?

这是上述内容的一个简单示例:

public interface A {
   void someMethod();
}
Run Code Online (Sandbox Code Playgroud)
public interface B extends A {
   void someMethod(); // why make this explicit?
   void anotherMethod();
}
Run Code Online (Sandbox Code Playgroud)

Zab*_*uza 8

一般的

一般来说,没有理由再次写出您已经从其他接口获得的方法。不要那样做。

Java文档

但是,在这种情况下Deque,它将覆盖 Javadoc,提供特定于Deque而不是一般性的更多详细信息Queue。

添加(E)

比较一下源码,例子add:

队列:

/**
 * Inserts the specified element into this queue if it is possible to do so
 * immediately without violating capacity restrictions, returning
 * {@code true} upon success and throwing an {@code IllegalStateException}
 * if no space is currently available.
 *
 * @param e the element to add
 * @return {@code true} (as specified by {@link Collection#add})
 * @throws IllegalStateException if the element cannot be added at this
 *         time due to capacity restrictions
 * @throws ClassCastException if the class of the specified element
 *         prevents it from being added to this queue
 * @throws NullPointerException if the specified element is null and
 *         this queue does not permit null elements
 * @throws IllegalArgumentException if some property of this element
 *         prevents it from being added to this queue
 */
boolean add(E e);
Run Code Online (Sandbox Code Playgroud)

双端队列:

/**
 * Inserts the specified element into the queue represented by this deque
 * (in other words, at the tail of this deque) if it is possible to do so
 * immediately without violating capacity restrictions, returning
 * {@code true} upon success and throwing an
 * {@code IllegalStateException} if no space is currently available.
 * When using a capacity-restricted deque, it is generally preferable to
 * use {@link #offer(Object) offer}.
 *
 * <p>This method is equivalent to {@link #addLast}.
 *
 * @param e the element to add
 * @return {@code true} (as specified by {@link Collection#add})
 * @throws IllegalStateException if the element cannot be added at this
 *         time due to capacity restrictions
 * @throws ClassCastException if the class of the specified element
 *         prevents it from being added to this deque
 * @throws NullPointerException if the specified element is null and this
 *         deque does not permit null elements
 * @throws IllegalArgumentException if some property of the specified
 *         element prevents it from being added to this deque
 */
boolean add(E e);
Run Code Online (Sandbox Code Playgroud)

差异:

不同之处

(由 P4Merge 创建的差异)

因此,当您使用Dequesadd方法时,您将看到另一个 Javadoc,而不是仅使用add另一个Queue.

  • @Zabuzard虽然编译器允许,但如果异常没有在超级接口/类中记录为可能的异常,则添加未经检查的异常会违反LSP。这就是为什么 Collections 框架充满了记录的可选异常,例如 `Collection.add` 指定 `NullPointerException`、`IllegalArgumentException` 和 `ClassCastException` 都是可能的异常 - 因为有子类需要抛出这些异常。 (2认同)
  • 这不仅仅是javadoc;这是关于 Queue 和 Deque 中方法的面向对象*契约*。Deque定义了一个更窄的契约,它完全符合Queue定义的契约。因此,保留了多态性:Deque 将按照 Queue 指定的方式运行,但 Deque 实际上遵守比 Queue 要求的更严格的定义。许多集合框架方法都这样做。 (2认同)