0

阅读相同的文章后,我仍然无法解决泛型问题:

我有基本活动:

abstract class BaseActivity : MvpAppCompatActivity(), BaseView {
    abstract fun getPresenter():BasePresenter<BaseView>
}

BaseView 接口

interface BaseView : MvpView

并且肯定 BasePresenter

open class BasePresenter<T : BaseView> : MvpPresenter<T>() 

然后我创建 BaseConnectionView

interface BaseConnectionView : BaseView

和 BaseConnectionPresenter

class BaseConnectionPresenter<T : BaseConnectionView> : BasePresenter<T>()

所以当我创建 BaseConnectionActivity

abstract class BaseConnectionActivity : BaseActivity(),BaseConnectionView {
    override abstract fun getPresenter(): BaseConnectionPresenter<BaseConnectionView>
}

我有错误:

Return type is BaseConnectionPresenter<BaseConnectionView>, 
which is not a subtype of overridden 
public abstract fun getPresenter():BasePresenter<BaseView>

它是亚型!

我怎么解决这个问题?

4

2 回答 2

1

BaseConnectionPresenter是 with 的一个子BasePresenter<T>类型T: BaseConnectionView。该函数getPresenter仅返回BasePresenter<BaseView>. 有问题,因为BasePresenter<T>不保证是BasePresenter<BaseView>。以下修复它:

class BaseConnectionPresenter<T : BaseConnectionView> : BasePresenter<BaseView>()
于 2017-11-21T14:18:59.860 回答
0

如果使用星形投影,解决方案比我想象的要容易

所以在 BaseActivity 我替换了

abstract fun getPresenter():BasePresenter<BaseView>

abstract fun getPresenter():BasePresenter<*>

然后我可以用新的演示者覆盖它,比如

override abstract fun getPresenter(): BaseConnectionPresenter<*>
于 2017-11-22T12:33:53.400 回答