我有一个带有泛型(T1)的抽象类,它扩展了我的可过滤接口:
public abstract class AbstractAdapter < T1 extends Filterable, T2 extends AbstractAdapter.Cell > extends ArrayAdapter < T1 > {
public abstract class Cell {
public T1 info;
//...
}
public abstract void setData(T2 cell, int position);
//...
}
我有一个带有方法(setData)和Cell类实现的具体类:
public class ConcreteAdapter extends AbstractAdapter < InfoClass, ConcreteAdapter.Cell > {
public class Cell extends AbstractAdapter.Cell {
//...
}
@Override
public void setData(Cell cell, int position) {
InfoClass info = (InfoClass)cell.info; // need to cast from Filterable to InfoClass (why?). Or i have compilation error
}
//...
}
所以,我有一个ConcreteAdapter类,它的第一个泛型类是InfoClass(扩展了Filterable),在方法setData中,我有Cell类对象,但字段"info"我看到的只是Filterable,而不是InfoClass。我认为,字段"info"应该已经是泛型类型了,因为它声明为
T1信息;
但不是
可过滤信息;
是否可以在没有强制转换的情况下将字段类型从可扩展类更改为泛型类?或者是IDE中的一个错误?
这是一个非常棘手的情况,因为AbstractAdapter
在AbstractAdapter.Cell
中是原始的。如果你指定了足够多的类型,那么你就不再需要强制转换了:
public abstract class AbstractAdapter < T1 extends Filterable, T2 extends AbstractAdapter<T1, T2>.Cell > extends ArrayAdapter < T1 > {
//...
}
和
public class ConcreteAdapter extends AbstractAdapter < InfoClass, ConcreteAdapter.Cell > {
public class Cell extends AbstractAdapter<InfoClass, ConcreteAdapter.Cell>.Cell {
//...
}
@Override
public void setData(Cell cell, int position) {
InfoClass info = cell.info;
}
//...
}
使其再次工作。然而,这是非常复杂的。如果可能的话,我会把Cell移到顶级类中。