java集合和接口_Java:设置接口和集合接口的区别

我只是查看了Set接口,发现它主要(或完全)仅重新声明了Collection接口中已经存在的函数。Set本身可以扩展Collection,这是否意味着Set接口自动具有的所有功能Collection?那么为什么要重新宣布呢?

例如,Set重新声明:

/**

* Returns the number of elements in this set (its cardinality). If this

* set contains more than Integer.MAX_VALUE elements, returns

* Integer.MAX_VALUE.

*

* @return the number of elements in this set (its cardinality)

*/

int size();

/**

* Returns true if this set contains no elements.

*

* @return true if this set contains no elements

*/

boolean isEmpty();

和声明在Collection:

/**

* Returns the number of elements in this collection. If this collection

* contains more than Integer.MAX_VALUE elements, returns

* Integer.MAX_VALUE.

*

* @return the number of elements in this collection

*/

int size();

/**

* Returns true if this collection contains no elements.

*

* @return true if this collection contains no elements

*/

boolean isEmpty();

这对我来说似乎很多余。为什么不将Set接口定义为:

public interface Set extends Collection {}

我认为这些接口之间没有任何区别,对吗?

当然,我不是在问的不同语义/含义Set。我知道。我只是问它在技术上(即对编译器)是否有任何区别。即,一般来说:

interface A { void foo(); }

interface B extends A { void foo(); }

interface C extends A {}

现在A,B或之间有什么区别C吗?

尽管某些功能(如add)的合同(即文档中所说的)确实可能有所不同,但有一个合理的理由要重新声明它们:能够放置新的文档,即定义新的合同。

但是,也有一些功能(如isEmpty)具有完全相同的文档/合同。为什么还要重新申报?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值