本人接触java不久,今天在看java集合体系的时候发现在一个问题,百思不得其解,故发帖求解:
在Collection接口中定义了一些方法,如size(),add(),clear()等等,而List接口继承自Collection接口,按正常的理解,在list中就不需要再写一次size(),add(),clear()这些方法,因为它会自动继承,但是在list中,它就还真的将这些方法重写了一次,不明白sun为什么要这么写

解决方案 »

  1.   

    list的size方法还是抽象方法 没有重写
      

  2.   

    Collection你自己都说了是接口了
    就是没实现的, 只是定义而已
    实现类,list必需去实现方法
      

  3.   

    你怎么知道list中又进行了重写,没有吧,应该是在JDK中拿出来介绍介绍而已吧!
      

  4.   

    估计是Collection包括List,Set的缘故吧,因为二者的这些方法本质上是不一样的,所以在继承一次吧,,
    楼主倒是挺细心的。
      

  5.   

    这是接口,接口中的方法都是abstract方法,list实现它就必须重写这些方法,不然的话就只能定义成abstract类啊
      

  6.   

    就像你说的那样是定义,那也没有必要重新再List接口中定义一次吧,它完全可以继承Collection中的吖
      

  7.   


    存在两个方法功能不完全一样:
    List<E>
    boolean add(E e)  向列表的尾部添加指定的元素(可选操作)。 Collection<E>
    boolean add(E e)  确保此 collection 包含指定的元素(可选操作)。 
      

  8.   

    看看API文档,List的每个方法的解释里都有一句
    Specified by:
    xxx in interface Collection<E>应该是方法的扩充吧?
      

  9.   

    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不是一个版本,一直在更新,所以它的扩展能力是必须的..
      

  10.   

    我看不出多写有什么意义,应该可以不写的,sun这样写那也无办法。
      

  11.   

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

  12.   

    size(),clear()这2个借口是没区别的
    但是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.
    将指定元素追加到此列表的结尾(可选操作)。
      

  13.   

    并且 collection  boolean add(E o)
    list voidadd(String item)
      

  14.   

    这么大的区别,你都看不出来? 
    Collection的add()只保证添加成功,添加到哪不管。
    List的add()保证添加到尾。好,我需要一个add()到最前面集合类,你用ArrayList实例调用接口的add()方法来处理下吧。
      

  15.   

    List接口是Collection的子接口 
    当然要重写Collection接口的所有方法
      

  16.   


    首先Collection是个接口类 List也是个接口 只是接口继承接口而已 并不是接口实现接口
    有些方法的确在一些抽象类里已经实现过了(AbstractList) 但这不代表子类就不能重写 虽然他们都实现了List接口 但是他们的部分实现方式是不一样的(LinkedList和ArrayList 一个是链表 一个是数组实现)
    所以父类实现过的方法不是不能重写(所以有些方法要通过抽象类实现通用功能 有些方法则交由子类去实现)
      

  17.   

    这个就是Java的面向对象的多态,楼主去好好理解一下,什么叫面向对象的多态
      

  18.   

    其实只是定义的接口行为不一样。
    表示子类如果实现List接口,那就需要遵循List Javadoc的行为。
    如果实现Collection那就必须要遵循Collection Javadoc的行为。
    不单单只是重写一个方法而已,重写这个方法的时候应该按照Javadoc所描述的一样。这样才正确。