在子接口中声明具有相同声明的通用方法的体系结构重要性是什么
what is the architecture importance of declaring common method having same declaration in child Interface
接口List & Collection 以及Set 和Collection 中声明了各种常用方法。
由于 List 和 Set 都扩展了 Collection,在子接口中声明具有相同声明的通用方法的架构重要性是什么。
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);//some more common method declaration
}
public interface List<E> extends Collection<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
}
这并没有打动我,我只想了解这样做背后的概念。
这不是架构原因。这是 'override' 文档的一种方式。例如,对于 List 中的大小方法,它们引用 'list',但对于 Colleciton 中的大小方法,它们引用 'collection'。其他方法相同:
集合源和 javadoc
/**
* Returns the number of elements in this collection. If this collection
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this collection
*/
int size();
/**
* Returns <tt>true</tt> if this collection contains no elements.
*
* @return <tt>true</tt> if this collection contains no elements
*/
boolean isEmpty();
列出源代码和 javadoc
/**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this list
*/
int size();
/**
* Returns <tt>true</tt> if this list contains no elements.
*
* @return <tt>true</tt> if this list contains no elements
*/
boolean isEmpty();
所以这只是一个好的文档的问题。
接口List & Collection 以及Set 和Collection 中声明了各种常用方法。 由于 List 和 Set 都扩展了 Collection,在子接口中声明具有相同声明的通用方法的架构重要性是什么。
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);//some more common method declaration
}
public interface List<E> extends Collection<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
}
这并没有打动我,我只想了解这样做背后的概念。
这不是架构原因。这是 'override' 文档的一种方式。例如,对于 List 中的大小方法,它们引用 'list',但对于 Colleciton 中的大小方法,它们引用 'collection'。其他方法相同:
集合源和 javadoc
/**
* Returns the number of elements in this collection. If this collection
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this collection
*/
int size();
/**
* Returns <tt>true</tt> if this collection contains no elements.
*
* @return <tt>true</tt> if this collection contains no elements
*/
boolean isEmpty();
列出源代码和 javadoc
/**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this list
*/
int size();
/**
* Returns <tt>true</tt> if this list contains no elements.
*
* @return <tt>true</tt> if this list contains no elements
*/
boolean isEmpty();
所以这只是一个好的文档的问题。