繁体   English   中英

如何避免施法?

[英]How to avoid casting?

我添加了新的全局高级别界面:

    public interface MvpView {
    }

我的高级界面:

public interface OfferDetailsView extends MvpView {

   public void showProgress();
}

另一个界面:

public interface OfferDetailsPdfMvp {

    interface View extends OfferDetailsView  {

        public void openLocalPdfFile(File file, int pageNumber);

    }

    interface Presenter extends OfferDetailsPresenter {

        public void onPageScrolled(int page, int pageCount);

    }
}

另一个界面:

public interface OfferDetailsPresenter extends MvpPresenter {

    public void downloadToAppDir(boolean isDownloadToAppCacheDir);

    public void onClickScreen(boolean isVisibleTopContainer);
}

另一个界面:

public interface MvpPresenter<V extends MvpView> {
    public void attachView(V mvpView);
}

基础抽象类:

public abstract class BaseOfferDetailsPresenter<T extends MvpView> implements MvpPresenter<T> {
    private T view;

     @Override
    public void attachView(T mvpView) {
        this.view = mvpView;
    }

   public T getView() {
         return view;
    }
}

并且从Base类扩展的conrete类:

public class OfferDetailsPdfPresenterImpl extends BaseOfferDetailsPresenter<OfferDetailsPdfMvp.View> implements OfferDetailsPdfMvp.Presenter {    

    public void viewIsReady() {
      getView().showProgress(); //no cast
      getView().openLocalPdfFile(file, currentPageNumber); // no cast
    }

但我得到编译错误:

myproject\app\src\main\java\com\myproject\android\customer\presenter\OfferDetailsPdfPresenterImpl.java:35: error: MvpPresenter cannot be inherited with different arguments: <> and <com.myproject.android.customer.mvp.OfferDetailsPdfMvp.View>
    public class OfferDetailsPdfPresenterImpl extends BaseOfferDetailsPresenter<OfferDetailsPdfMvp.View> implements OfferDetailsPdfMvp.Presenter {

你不能 - 用那个签名。

请记住, getView()说:

OfferDetailsView getView()

因此:此方法的签名使用您的一个基接口。

只有两种选择:

  • 将返回类型更改为更具体的子接口/类
  • 做一个checkof check + cast

只是假设 getView()返回一些特定的类和具有投选中是不是好的做法。 另一方面:当很明显getView()将返回OfferDetailsPdfMvp的实例时, OfferDetailsPdfMvp地更改方法签名应该不是什么大问题。

要制作您想要的内容,您应该更改OfferDetailsPresenter签名:

public interface OfferDetailsPresenter<T extends MvpView> extends MvpPresenter<T> {

    void downloadToAppDir(boolean isDownloadToAppCacheDir);

    void onClickScreen(boolean isVisibleTopContainer);
}

然后,在OfferDetailsPdfMvp内部,您应该以相同的方式更改Presenter签名:

public interface OfferDetailsPdfMvp {

    interface View extends OfferDetailsView  {

        public void openLocalPdfFile(File file, int pageNumber);

    }

    interface Presenter<T extends MvpView> extends OfferDetailsPresenter<T> {

        public void onPageScrolled(int page, int pageCount);

    }
}

然后像使用BaseOfferDetailsPresenter<T extends MvpView>一样填充所需的View

OfferDetailsPdfPresenterImpl extends BaseOfferDetailsPresenter<OfferDetailsPdfMvp.View> implements OfferDetailsPdfMvp.Presenter<OfferDetailsPdfMvp.View>

getView()返回类型OfferDetailsView ,它没有名为openLocalPdfFile的方法。

也许你想让getView()返回OfferDetailsPdfMvp.View

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM