接口List&Collection以及Set和Collection中声明了各种常用方法。
由于List和Set都扩展了Collection,因此在子接口中声明具有相同声明的通用方法的体系结构重要性是什么。public interface Collection extends Iterable {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);//some more common method declaration
}
public interface List extends Collection {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
}
这并没有让我感到震惊,我只想知道这样做背后的概念。
最佳答案
这不是体系结构的原因。这是一种“覆盖”文档的方式。例如,对于List中的size方法,它们引用“列表”,而对于Colleciton中的size方法,它们引用“集合”。其他方法相同:
集合源和Javadoc/**
* 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();
列出源代码和Javadoc
/**
* Returns the number of elements in this list. If this list contains
* more than Integer.MAX_VALUE elements, returns
* Integer.MAX_VALUE.
*
* @return the number of elements in this list
*/
int size();
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
boolean isEmpty();
因此,这只是好的文档问题。