我有一個接口,並實現它的類:不能通過的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