我有一个接口,并实现它的类:不能通过的ArrayList <Impl>到方法采取的ArrayList <Interface>
public interface IFoo{
Integer bar();
}
public class Foo implements IFoo{
@Override
public Integer bar(){
return 42;
}
}
现在,在另一个类,我有一个函数,该函数的接口类型的ArrayList。
public class Something {
public static void function(ArrayList<IFoo> foos){
//do something on foos
}
}
我想用这个函数foo的数组列表:
ArrayList<Foo> myFoos = new ArrayList<Foo>();
Something.function(myFoos);//can NOT do this
有什么问题或者我该怎么办?
稍微偏离主题,但为什么不'公共静态无效的功能(的IFoo FOOS ...)'? – Leon 2014-09-29 05:42:37
我同意@Leon。编码到*界面*是一个好习惯。 – TheLostMind 2014-09-29 05:43:41
你的界面名称是IFoo,而你生成的arraylist是Foo。但是在函数签名里面的Something类中你已经提到了正确的。 – 2014-09-29 05:43:49