这不应该是可能的吗?如果没有,那么有没有办法类似地做到这一点?
public abstract class BaseItem {
}
public class SpecificItem extends BaseItem {
public int value;
}
public class AnAdapter{
public AnAdapter(SparseArray<BaseItem> items){
}
}
public class DifferenClass{
public DifferenClass(){
SparseArray<SpecificItem> items = new SparseArray<SpecificItem>();
AnAdapter anAdapter = new AnAdapter(items); // here is wrong type
}
}
-------- 编辑:更新。是否是将特定元素附加到更通用容器的唯一方法?所有这些铸件看起来有点脏。但是工作...
public abstract class BaseItem {
}
public class SpecificItem extends BaseItem {
public int value;
}
public class AnAdapter{
public AnAdapter(SparseArray<? extends BaseItem> items){
}
}
public class DifferenClass{
DifferenClass(){
SparseArray<? extends BaseItem> items = new SparseArray<BaseItem>();
((SparseArray<SpecificItem>)items).append(0,new SpecificItem());
}
}