实现泛型类型接口

时间:2018-02-28 15:32:11

标签: java generics interface

假设我有以下界面:

public interface MvpView { }

public interface MvpPresenter<V extends MvpView> {
    void attach(V view);
}

然后是实现MvpPresenter

的基类
public class BasePresenter<V extends MvpView> implements MvpPresenter<V> {
    private V view;

    @Override
    public void attach(V view) {
        this.view = view;
    }
}

最后是以下扩展BasePresenter的类

public abstract class BaseFragment<P extends MvpPresenter> implements MvpView {
    P presenter;

    public void someMethod() {
        presenter.attach(this);
    }
}

虽然这会编译它并不安全,因为无法保证类的实现会实现正确的MvpViewP presenter使用的那个)。为了安全起见,我遗漏了someMethod的代码,并且每个实现都必须使用完全相同的代码填充presenter.attach(this)

我想做什么,但我不知道如何如下:

public abstract class BaseFragment<V extends MvpView, P extends MvpPresenter<V>> implements V {
    P presenter;

    public void someMethod() {
        presenter.attach(this);
    }
}

有没有办法实现这个目标?

3 个答案:

答案 0 :(得分:3)

像这样声明BaseFragment

abstract class BaseFragment<P extends MvpPresenter<R>, R extends BaseFragment<P,R>> implements MvpView {
    P presenter;

   public void someMethod() {
        presenter.attach(getThis());
   }

   abstract R getThis();
   }
}

并使用像这样的扩展程序

class MyFragment extends BaseFragment<MvpPresenter<MyFragment>, MyFragment> {

    @Override
    MyFragment getThis() {
        return this;
    }   

}

答案 1 :(得分:1)

编译:

public abstract class BaseFragment<P extends MvpPresenter<BaseFragment>> implements MvpView {

    P presenter;

    public void someMethod() {
        presenter.attach(this);
    }
}

但我还没有找到一种实际的子类化方法。我不确定MvpView是否真的需要任何东西。也许这样的事情会很有用:

public interface MvpPresenter<V> {

    void attach(V view);
}

public class BasePresenter<V> implements MvpPresenter<V> {
    private V view;

    @Override
    public void attach(V view) {
        this.view = view;
    }
}

public abstract class BaseFragment<R extends BaseFragment>  {

    MvpPresenter<R> presenter;

    public void someMethod() {
        presenter.attach(getThis());
    }

    abstract R getThis();
}

public class MyBaseFragment extends BaseFragment<MyBaseFragment> {

    @Override
    MyBaseFragment getThis() {
        return this;
    }

}

答案 2 :(得分:0)

根据boobalan gnanasekaran和user158037的答案,我这样做了:

public interface MvpView { }

public interface MvpPresenter<V extends MvpView> {
    void attach(V view);
}

public class BasePresenter<V extends MvpView> implements MvpPresenter<V> {
    private V view;

    @Override
    public void attach(V view) {
        this.view = view;
    }

    public V getView() {
        return view;
    }
}

public abstract class BaseFragment<V extends MvpView, P extends MvpPresenter<V>> {
    P presenter;

    protected abstract V getThis();

    public void someMethod() {
        presenter.attach(getThis());
    }

    public P getPresenter() { return presenter; }
}

一个示例实现

public interface MoviesContract {
    interface Presenter extends MvpPresenter<MoviesContract.View> {
        void loadMovies();
    }

    interface View extends MvpView {
        void onMoviesLoaded();
    }
}

public class MoviesPresenter extends BasePresenter<MoviesContract.View>
    implements MoviesContract.Presenter {

    @Override
    public void loadMovies() {
        getView.onMoviesLoaded();
    }
}


public class MoviesFragment extends BaseFragment<MoviesContract.View, MoviesContract.Presenter>
    implements MoviesContract.View {

    @Override
    public MoviesContract.View getThis() {
        return this;
    }

    public loadMovies() {
        getPresenter().loadMovies();
    }

    @Override
    public void onMoviesLoaded() {
        //
    }
}