0

我对泛型类型、子类型和它们之间的不匹配有疑问。我有特定的结构类和接口。我会告诉你,请解释为什么会发生类型不匹配。

假设我正在准备我的 MVP 框架,并且我有以下接口和类:

这是最高的抽象

interface Presenter<in V : AbstractView> {
    fun attachView(view: V)

    fun detachView()

    fun onDestory() {

    }
}

抽象类包含Presenter的具体方法和实现

abstract class AbstractPresenter<V : AbstractView> : Presenter<V>, LifecycleObserver {

    private var viewReference: WeakReference<V?>? = null

    protected abstract fun onAttached(view: V)

    final override fun attachView(view: V) {
        viewReference = WeakReference(view)
        onAttached(view)
    }

    final override fun detachView() {
        viewReference?.clear()
        viewReference = null
        onDetached()
    }

    protected open fun onDetached() {
    }
}

合同

interface DashboardContract {

    interface View : AbstractView {

    }

    abstract class Presenter : AbstractPresenter<View>(){

    }
}

最后

class DashboardPresenter : DashboardContract.Presenter() {

    override fun onAttached(view: DashboardContract.View) {
    }
}

就它而言,AbstractView它看起来更简单。只有界面AbstractView。在合同中DashboardContract.View扩展AbstractView了接口,我DashboardActivity实现了这个DashboardContract.View接口。

class DashboardActivity : BaseActivity(), DashboardContract.View { ... }

所以当我在我的和创建方法中创建DashboardPresenter一个属性时,我得到了为什么?不是 的子类型吗?DashboardActivityfun getPresenter() : Presenter<AbstractView>Type mismatch errorPresenter<AbstractView>

fun getPresenter() : AbstractPresenter<AbstractView> {
        return dashboardPresenter // The type is DashboardPresenter
    }

让我们看一下Java代码:

我正在看反编译 Kotlin 的 Java 代码。我把它放在下面。这是看起来的Presenter样子:

public interface Presenter {
   void attachView(@NotNull AbstractView var1);

   void detachView();

   void onDestory();

   @Metadata(...)
   public static final class DefaultImpls {
      public static void onDestory(Presenter $this) {
      }
   }
}

我认为如果我在 Kotlin 中使用泛型类,我也会在 java 中获得泛型类。我错了。

AbstractPresenter给出:

public abstract class AbstractPresenter implements Presenter, LifecycleObserver {
   private WeakReference viewReference;

   protected abstract void onAttached(@NotNull AbstractView var1);

   public final void attachView(@NotNull AbstractView view) {
      Intrinsics.checkParameterIsNotNull(view, "view");
      this.viewReference = new WeakReference(view);
      this.onAttached(view);
   }

   public final void detachView() {
      WeakReference var10000 = this.viewReference;
      if(this.viewReference != null) {
         var10000.clear();
      }

      this.viewReference = (WeakReference)null;
      this.onDetached();
   }

   protected void onDetached() {
   }

   public void onDestory() {
      DefaultImpls.onDestory(this);
   }
}

合同

public interface DashboardContract {
   @Metadata(...)
   public interface View extends AbstractView {
   }

   @Metadata(...)
   public abstract static class Presenter extends AbstractPresenter {
   }
}

DashboardPresetner:_

public final class DashboardPresenter extends Presenter {

   protected void onAttached(@NotNull View view) {
      Intrinsics.checkParameterIsNotNull(view, "view");
   }

   // $FF: synthetic method
   // $FF: bridge method
   public void onAttached(AbstractView var1) {
      this.onAttached((View)var1);
   }
}
4

1 回答 1

0

您必须更改 in 的父级PresenterDashboardContract使用AbstractView而不是View

abstract class Presenter : AbstractPresenter<AbstractView>()

我不确定为什么不允许您使用View,这可能是 Kotlin 递归类型检查中的一个缺陷。看看相应的 java 代码是什么并继续调查可能会很有趣。

于 2018-03-10T19:39:50.670 回答