我有以下类/接口(interface):
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);
}
}
更准确地说,我不能用给定的参数调用父类(super class)的构造函数:
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
}
}
我需要更改什么?
最佳答案
尝试以这种方式更改 GenericPresenter
类:
private GenericViewModel<? extends AbstractDatabaseObject> model;
private IGenericView view;
public GenericPresenter(GenericViewModel<? extends AbstractDatabaseObject> model,
IGenericView view) {
this.model = model;
this.view = view;
view.addListener(this);
}
关于Java继承和泛型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30481912/