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)
一般来说,没有理由再次写出您已经从其他接口获得的方法。不要那样做。
但是,在这种情况下Deque,它将覆盖 Javadoc,提供特定于Deque而不是一般性的更多详细信息Queue。
比较一下源码,例子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.