gpt4 book ai didi

Java继承和泛型问题

转载 作者:行者123 更新时间:2023-11-30 11:03:16 25 4
gpt4 key购买 nike

我有以下类/接口(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com