Kotlin函数不需要,但定义为其他类型

Raf*_*afa 2 generics android kotlin kotlin-extension kotlin-android-extensions

我已经定义了这样的课程

abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<out Any?, out Any?> {
    protected var presenter: P? = null

    fun bindPresenter(presenter: P): Unit {
        this.presenter = presenter
        presenter.bindView(itemView)
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里presenter.bindView(itemView)给我一个错误说明Type mismatch, required: Nothing, found: View!。我已经像这样定义bindViewpresenter类的内部

abstract class BasePresenter<M, V> {
     var view: WeakReference<V>? = null
     var model: M? = null

     fun bindView(view: V) {
        this.view = WeakReference(view)
    }
}
Run Code Online (Sandbox Code Playgroud)

它的取值为view: V

我尝试定义BasePresenter<out Any?, out Any?>使用星型语法的扩展名,BasePresenter<*,*>但出现相同的错误。我也尝试过简单地使用BasePresenter<Any?, Any?>它来解决直接问题,但随后扩展的内容却P: BasePresenter<Any?, Any?>给出一个错误,说它在期待P,但是得到了BasePresenter<Any?, Any?>

这是一个在我的代码中发生的示例

abstract class MvpRecyclerListAdapter<M, P : BasePresenter<Any?, Any?>, VH : MvpViewHolder<P>> : MvpRecyclerAdapter<M, P, VH>() {...}
Run Code Online (Sandbox Code Playgroud)

在这一行上,我会在扩展部分得到上面提到的错误 MvpRecyclerAdapter<M, P, VH>

我似乎无法解决这个问题。我该如何解决?

Lym*_*Zoy 5

你已经宣布对泛型参数VBasePresenter<out Any?, out Any?>,所以presenter.bindView不能采用输入参数。

解决方案:将声明更改为BasePresenter<out Any?, View?>

查看官方文档以获取更多信息。