Ank*_*kur 2 java collections arraylist interface-implementation
我想通过一些ArrayList<Integer>
X进入方法a(Collection<Integer> someCol)
是采用Collection<Integer>
作为输入.
我怎样才能做到这一点?我认为ArrayList是一个集合,因此我应该能够"只做它"但似乎Collection是一个接口,而ArrayList实现了这个接口.我能做些什么来使这项工作......如果你理解这个理论对我和其他许多人也有帮助.
谢谢
去做就对了.
说真的,一个类会隐式地转换为它实现的接口.
编辑
如果你需要一个例子:
import java.util.*;
public class Sandbox {
public static void main(String[] args) {
final ArrayList<Integer> list = new ArrayList<Integer>(5);
Collections.addAll(list, 1, 2, 3, 4, 5);
printAll(list);
}
private static void printAll(Collection<Integer> collection) {
for (Integer num : collection)
System.out.println(num);
}
}
Run Code Online (Sandbox Code Playgroud)