简体   繁体   English

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

[英]Interfaces extending other interfaces but also containing same methods

Just looking Java Collections and I see the List interface, which extends the Collection interface, also contains the exact same method signatures in the Collection interface -- but not all of them just a subset.只看 Java Collections 我看到了 List 接口,它扩展了 Collection 接口,在 Collection 接口中也包含完全相同的方法签名——但并非所有这些都只是一个子集。 My question is why?我的问题是为什么? Since it extends it, we know it will inherit them all by default.由于它扩展了它,我们知道它将默认继承它们。 And why a subset?为什么是子集?

The same can be seen with the Deque extending the Queue, containing all the method signatures of the Queue.同样可以看到扩展队列的双端队列,包含队列的所有方法签名。 Doesn't this make it harder to know what's unique about that interface?这不会使了解该界面的独特之处变得更加困难吗?

Here is a simple example of the above:这是上面的一个简单示例:

public interface A {
   void someMethod();
}
public interface B extends A {
   void someMethod(); // why make this explicit?
   void anotherMethod();
}

General一般的

In general there is no reason to write out the methods you already get from the other interface again.一般来说,没有理由再次写出您已经从另一个接口获得的方法。 Do not do that.不要那样做。

Javadoc Javadoc

In case of Deque however, it is to override the Javadoc, giving more details that are specific to Deque and not to general Queue s.然而,在Deque的情况下,它将覆盖 Javadoc,提供更多特定于Deque而不是一般Queue的细节。

add(E)添加(E)

Compare the source code, example add :对比源码,示例add

Queue : 队列

/**
 * 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);

Deque : 双端队列

/**
 * 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);

The diff:差异:

区别

(diff created by P4Merge) (由 P4Merge 创建的差异)

So when you use Deque s add method, you will see another Javadoc than when just using the add method of another Queue .因此,当您使用Dequeadd方法时,您将看到另一个 Javadoc,而不是仅使用另一个Queueadd方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM