2
我有下面的類/接口:Java繼承和通用問題
public class GenericViewModel<T extends AbstractDatabaseObject> {
private Class<?> type;
@SuppressWarnings("unchecked")
public GenericViewModel(Class<?> cl) {
type = cl;
}
}
和專業化:
public class PersonViewModel extends GenericViewModel<Person> implements IPersonViewModel{
public PersonViewModel() {
super(Person.class);
}
}
現在,我的問題是,在主持人:
public class GenericPresenter implements IGenericView.IGenericViewListener {
private GenericViewModel<AbstractDatabaseObject> model;
private IGenericView view;
public GenericPresenter(GenericViewModel<AbstractDatabaseObject> model, IGenericView view) {
this.model = model;
this.view = view;
view.addListener(this);
}
}
更確切地說,我不能用給定的參數調用超類的構造函數:
public class PersonPresenter extends GenericPresenter {
PersonViewModel model;
IPersonView view;
public PersonPresenter(PersonViewModel model, IPersonView view) {
super(model, view); // Here is the problem. No such constructor in superclass found
// IGenericView i = (IGenericView) view; <-- this seems to work
// GenericViewModel<AbstractDatabaseObject> m = model; <-- this doesn't
}
}
我必須改變什麼?
您是否嘗試過'私人GenericViewModel <?擴展AbstractDatabaseObject> model;'? –
您是否嘗試直接投射到'super':'super((GenericViewModel)模型,(IGenericView)視圖);' –
romfret