Java Generics - 抽象类抽象函数覆盖

时间:2016-09-26 11:15:33

标签: java android generics abstract-class

我正在尝试在android中实现基本抽象类。

我的基本抽象类是BaseItemPresenter类。课程如下:

public abstract class BaseItemPresenter<T extends MvpView> extends BasePresenter<T> {

    public abstract void loadFromDevice();

    public abstract void loadFromFile();

    public abstract void backup(List<? extends BaseBackupItem> list);

    public abstract void delete(List<? extends BaseBackupItem> list);

    public abstract void restore(List<? extends BaseBackupItem> list);

    public abstract void sort(int sortType, boolean isAscending);

    public abstract void sort(List<? extends BaseBackupItem> list, int sortType, boolean isAscending);

    public abstract void filter(List<? extends BaseBackupItem> list, Class<? extends MvpView> filterOptions);
}

我正在用ItemPresenter1类实现这个类。代码如下:

public class ItemPresenter1 extends BaseItemPresenter<MvpView> {

    @Override
    public void loadFromDevice() {

    }

    @Override
    public void loadFromFile() {

    }

    @Override
    public void backup(List<Item1> list) {

    }

    @Override
    public void delete(List<Item1> list) {

    }

    @Override
    public void restore(List<Item1> list) {

    }

    @Override
    public void sort(int sortType, boolean isAscending) {

    }

    @Override
    public void sort(List<Item1> list, int sortType, boolean isAscending) {

    }

    @Override
    public void filter(List<Item1> list, Class<View1> filterOptions) {

    }
}

Item1类正在扩展BaseBackupItem类。 View1类正在扩展MvpView类。

public class Item1 extends BaseBackupItem {

}

public class View1 extends MvpView {

}

所以,我应该如何解决这个问题:

来自:

public abstract void backup(List<? extends BaseBackupItem>);

到:

@Override
public void backup(List<Item1>);

错误:

Method does not override method from its superclass.

1 个答案:

答案 0 :(得分:1)

您需要在BaseItemPresenter的班级定义中添加新类型参数,以便将List的预期类型指定为下一个:

public abstract class BaseItemPresenter<T extends MvpView, I extends BaseBackupItem> 
    extends BasePresenter<T> {
    ...
    public abstract void backup(List<I> list);

    public abstract void delete(List<I> list);

    public abstract void restore(List<I> list);

    ...
}

然后您的班级ItemPresenter1将被声明为下一个:

public class ItemPresenter1 extends BaseItemPresenter<MvpView, Item1 > {
    ...
    @Override
    public void backup(List<Item1> list) {