专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > J2SE

List接口中为什么会有Collection接口中存在的方法?解决方案

发布时间:2010-06-05 05:28:46 文章来源:www.iduyao.cn 采编人员:星星草
List接口中为什么会有Collection接口中存在的方法????
本人接触java不久,今天在看java集合体系的时候发现在一个问题,百思不得其解,故发帖求解:
在Collection接口中定义了一些方法,如size(),add(),clear()等等,而List接口继承自Collection接口,按正常的理解,在list中就不需要再写一次size(),add(),clear()这些方法,因为它会自动继承,但是在list中,它就还真的将这些方法重写了一次,不明白sun为什么要这么写

------解决方案--------------------
lz
你说的是重写还是重新定义 
我暂且理解为重新定义 
我也不明白为什么 接口和类不知道有没有区别
但是子类如果重新定义父类方法,会覆盖父类的 方法 接口里不知道一样不一样
如果一样 写了编译能过去,也可以理解

------解决方案--------------------
估计是Collection包括List,Set的缘故吧,因为二者的这些方法本质上是不一样的,所以在继承一次吧,,
楼主倒是挺细心的。
------解决方案--------------------
这是接口,接口中的方法都是abstract方法,list实现它就必须重写这些方法,不然的话就只能定义成abstract类啊
------解决方案--------------------
探讨

有必要在子类中将父亲中已有的方法拿来重新介绍一次么?

------解决方案--------------------
看看API文档,List的每个方法的解释里都有一句
Specified by:
xxx in interface Collection<E>

应该是方法的扩充吧?
------解决方案--------------------
public interface List<E>extends Collection<E>


boolean add(E e)

指定者:
接口 Collection<E> 中的 add
参数:
e - 要添加到列表的元素 
返回:
true(根据 Collection.add(E) 的规定) 
抛出: 
UnsupportedOperationException - 如果列表不支持 add 操作 
ClassCastException - 如果指定元素的类不允许它添加到此列表 
NullPointerException - 如果指定的元素为 null,并且此列表不允许 null 元素 
IllegalArgumentException - 如果此元素的某些属性不允许它添加到此列表


方法重写, 它的作用不用多说吧, java不是一个版本,一直在更新,所以它的扩展能力是必须的..
------解决方案--------------------
size(),clear()这2个借口是没区别的
但是add()方法是有区别的
Collection/add()(源码注释)
Java code
Ensures that this collection contains the specified element (optional
     operation).  Returns <tt>true</tt> if this collection changed as a
     result of the call.  (Returns <tt>false</tt> if this collection does
     not permit duplicates and already contains the specified element.)<p>
     
     Collections that support this operation may place limitations on what
     elements may be added to this collection.  In particular, some
      collections will refuse to add <tt>null</tt> elements, and others will
      impose restrictions on the type of elements that may be added.
      Collection classes should clearly specify in their documentation any
      restrictions on what elements may be added.<p>
     
     If a collection refuses to add a particular element for any reason
     other than that it already contains the element, it <i>must</i> throw
      an exception (rather than returning <tt>false</tt>).  This preserves
     the invariant that a collection always contains the specified element
     after this call returns.
确保此 collection 包含指定的元素(可选操作)。如果此 collection 随调用的结果而发生改变,则返回 true。(如果此 collection 不允许有重复元素,并且已经包含了指定的元素,则返回 false。)
支持此操作的 collection 可以限制哪些元素能添加到此 collection 中来。需要特别指出的是,一些 collection 拒绝添加 null 元素,其他一些 collection 将对可以添加的元素类型强加限制。Collection 类应该在其文档中清楚地指定能添加哪些元素方面的所有限制。

如果 collection 由于某些原因(已经包含该元素的原因除外)拒绝添加特定的元素,那么它必须 抛出一个异常(而不是返回 false)。这确保了在此调用返回后,collection 总是包含指定的元素。

------解决方案--------------------
探讨
恕小弟愚昧,虽然API说这两个方法的功能不一样,但它们都是接口,到具体的实现它们的功能不是一样的么?
就拿ArrayList来说,不管它是怎么实现的,Collection<String> cs = new ArrayList<String>()与List<String> ls = new ArrayList<Stirng>;的add()方法调用的是同一个吧

------解决方案--------------------
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: