gpt4 book ai didi

java - 扩展其他接口(interface)但也包含相同方法的接口(interface)

转载 作者:行者123 更新时间:2023-12-05 09:01:17 25 4
gpt4 key购买 nike

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

Deque 扩展了 Queue,包含了 Queue 的所有方法签名。这不会让了解该界面的独特之处变得更加困难吗?

这是上面的一个简单例子:

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

最佳答案

一般

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

Java文档

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

添加(E)

对比源码,例子添加:

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

区别:

difference

(由 P4Merge 创建的差异)

因此,当您使用 Dequeadd 方法时,您将看到另一个 Javadoc,而不是仅使用另一个 add 方法队列

关于java - 扩展其他接口(interface)但也包含相同方法的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73517501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com