Java继承和泛型问题

时间:2015-05-27 12:05:06

标签: java generics inheritance

我有以下类/接口:

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


     }
}

我需要改变什么?

1 个答案:

答案 0 :(得分:1)

尝试以这种方式更改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);
}