本人接触java不久,今天在看java集合体系的时候发现在一个问题,百思不得其解,故发帖求解:
在Collection接口中定义了一些方法,如size(),add(),clear()等等,而List接口继承自Collection接口,按正常的理解,在list中就不需要再写一次size(),add(),clear()这些方法,因为它会自动继承,但是在list中,它就还真的将这些方法重写了一次,不明白sun为什么要这么写
在Collection接口中定义了一些方法,如size(),add(),clear()等等,而List接口继承自Collection接口,按正常的理解,在list中就不需要再写一次size(),add(),clear()这些方法,因为它会自动继承,但是在list中,它就还真的将这些方法重写了一次,不明白sun为什么要这么写
就是没实现的, 只是定义而已
实现类,list必需去实现方法
楼主倒是挺细心的。
存在两个方法功能不完全一样:
List<E>
boolean add(E e) 向列表的尾部添加指定的元素(可选操作)。 Collection<E>
boolean add(E e) 确保此 collection 包含指定的元素(可选操作)。
Specified by:
xxx in interface Collection<E>应该是方法的扩充吧?
boolean add(E e)指定者:
接口 Collection<E> 中的 add
参数:
e - 要添加到列表的元素
返回:
true(根据 Collection.add(E) 的规定)
抛出:
UnsupportedOperationException - 如果列表不支持 add 操作
ClassCastException - 如果指定元素的类不允许它添加到此列表
NullPointerException - 如果指定的元素为 null,并且此列表不允许 null 元素
IllegalArgumentException - 如果此元素的某些属性不允许它添加到此列表
方法重写, 它的作用不用多说吧, java不是一个版本,一直在更新,所以它的扩展能力是必须的..
就拿ArrayList来说,不管它是怎么实现的,Collection<String> cs = new ArrayList<String>()与List<String> ls = new ArrayList<Stirng>;的add()方法调用的是同一个吧
但是add()方法是有区别的
Collection/add()(源码注释)
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 总是包含指定的元素。List/add()(源码注释)
Appends the specified element to the end of this list (optional
operation).
<p>Lists that support this operation may place limitations on what
elements may be added to this list. In particular, some
lists will refuse to add null elements, and others will impose
restrictions on the type of elements that may be added. List
classes should clearly specify in their documentation any restrictions
on what elements may be added.
将指定元素追加到此列表的结尾(可选操作)。
list voidadd(String item)
Collection的add()只保证添加成功,添加到哪不管。
List的add()保证添加到尾。好,我需要一个add()到最前面集合类,你用ArrayList实例调用接口的add()方法来处理下吧。
当然要重写Collection接口的所有方法
首先Collection是个接口类 List也是个接口 只是接口继承接口而已 并不是接口实现接口
有些方法的确在一些抽象类里已经实现过了(AbstractList) 但这不代表子类就不能重写 虽然他们都实现了List接口 但是他们的部分实现方式是不一样的(LinkedList和ArrayList 一个是链表 一个是数组实现)
所以父类实现过的方法不是不能重写(所以有些方法要通过抽象类实现通用功能 有些方法则交由子类去实现)
表示子类如果实现List接口,那就需要遵循List Javadoc的行为。
如果实现Collection那就必须要遵循Collection Javadoc的行为。
不单单只是重写一个方法而已,重写这个方法的时候应该按照Javadoc所描述的一样。这样才正确。