繁体   English   中英

接口和实现方法

[英]Interface and implemented methods

我有一个问题。 基于Java 7 API的Collection是一个接口,但是它带有一些具体的方法,例如size()。 我不明白,该接口如何包含已实现的方法。 如果那是一个抽象类,那是有道理的。 最好的祝福

集合是一个接口,但它带有一些具体方法,例如size()。

这不是真的。 您已经知道一个接口仅定义合同,并将实现留给实现它的类。 如果您指的是类似

Collection<String> collection = new ArrayList<String>();
System.out.println("Size of the collection is: " + collection.size());

请注意, size()实现是由ArrayList而不是Collection

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();

没有任何方法的具体实现。 您所指的方法, 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM