我这里有一个问题。基于Java7的API Collection是一个接口,但是它附带了一些具体的方法,比如size()。我不明白,接口是如何包含已实现的方法的。如果这是一个抽象类,那就有意义了。诚挚的问候
发布于 2013-07-06 23:09:31
size集合是一个接口,但是它附带了一些具体的方法,比如
()。
这不是真的。您已经知道,接口只是定义了约定,而将实现留给了实现它的类。如果你指的是像这样的
Collection<String> collection = new ArrayList<String>();
System.out.println("Size of the collection is: " + collection.size());请注意,size()实现是由ArrayList而不是Collection提供的。
发布于 2013-07-06 22:48:03
java.util.Collection没有实现的方法,它是一个接口。下面是size方法的声明:
/**
* 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();发布于 2013-07-06 22:49:01
没有任何方法的具体实现。您所指的方法,size也没有任何具体的实现。
/**
* 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();https://stackoverflow.com/questions/17503998
复制相似问题