对JDK源码的疑惑(也是对接口的疑惑)

kewin 2004-10-19 10:21:08
最近在看JDK collection 的源码,看到
Set extends Collection 时重复写了全部的接口。这是为什么?
附代码片断:
public interface Set extends Collection {
// Query Operations

/**
* Returns the number of elements in this set (its cardinality). If this
* set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this set (its cardinality).
*/
int size();

/**
* Returns <tt>true</tt> if this set contains no elements.
*
* @return <tt>true</tt> if this set contains no elements.
*/
boolean isEmpty();

/**
* Returns <tt>true</tt> if this set contains the specified element. More
* formally, returns <tt>true</tt> if and only if this set contains an
* element <code>e</code> such that <code>(o==null ? e==null :
* o.equals(e))</code>.
*
* @param o element whose presence in this set is to be tested.
* @return <tt>true</tt> if this set contains the specified element.
* @throws ClassCastException if the type of the specified element
* is incompatible with this set (optional).
* @throws NullPointerException if the specified element is null and this
* set does not support null elements (optional).
*/
boolean contains(Object o);
.....................................
...全文
140 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
joy_wind 2004-10-21
  • 打赏
  • 举报
回复
同意 Polarislee 和 chDW 的说法,为了检查源代码的方便。

另外我猜还有一个原因就是保持Set的相对独立性:如果SUN以后要做api改造,比如Collection类要做修改,那么那些使用Set接口的用户就可以不受影响。这看似多余的做法就体现出好处来啦。
ChDw 2004-10-20
  • 打赏
  • 举报
回复
其实只是为了使在看代码的时候更清楚而已,不是必须的
hocus 2004-10-20
  • 打赏
  • 举报
回复
SET内元素是不可重复的
这是对collection的扩充约束
北极猩猩 2004-10-20
  • 打赏
  • 举报
回复
就是让你看源代码的时侯能知道全部的函数,而不用再去查看父接口
SIVCORE 2004-10-20
  • 打赏
  • 举报
回复
在我看来,A是对B的扩展,就像类扩展一样
sgdb 2004-10-20
  • 打赏
  • 举报
回复
说明b还有别的用处
kewin 2004-10-20
  • 打赏
  • 举报
回复
///
SET内元素是不可重复的
这是对collection的扩充约束
/////
可是Set 是个interface,没有任何具体的实现呀。

zouyiyy 2004-10-19
  • 打赏
  • 举报
回复
顶一下 期待高人来解答
Tasia 2004-10-19
  • 打赏
  • 举报
回复
有和你一样的疑惑。
kewin 2004-10-19
  • 打赏
  • 举报
回复
一个interface A extends 另一个interface B 时,
A会自动获得B的所有接口,为什么要重写呢?!这样做的好处在哪里?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧