Kotlin:解决泛型继承
Kotlin: resolve generic inheritance
阅读相同的文章后,我仍然无法解决泛型问题:
我有 BaseActivity:
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>
但是它是亚型!
我该如何解决这个问题?
BaseConnectionPresenter
是 BasePresenter<T>
和 T: BaseConnectionView
的子类型。函数getPresenter
只有returnsBasePresenter<BaseView>
。有一个问题,因为 BasePresenter<T>
不一定是 BasePresenter<BaseView>
。以下修复它:
class BaseConnectionPresenter<T : BaseConnectionView> : BasePresenter<BaseView>()
如果使用星形投影,解决方案比我想象的要简单
所以我在 BaseActivity 中替换了
abstract fun getPresenter():BasePresenter<BaseView>
到
abstract fun getPresenter():BasePresenter<*>
然后我可以用新的演示者覆盖它,比如
override abstract fun getPresenter(): BaseConnectionPresenter<*>
阅读相同的文章后,我仍然无法解决泛型问题:
我有 BaseActivity:
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>
但是它是亚型!
我该如何解决这个问题?
BaseConnectionPresenter
是 BasePresenter<T>
和 T: BaseConnectionView
的子类型。函数getPresenter
只有returnsBasePresenter<BaseView>
。有一个问题,因为 BasePresenter<T>
不一定是 BasePresenter<BaseView>
。以下修复它:
class BaseConnectionPresenter<T : BaseConnectionView> : BasePresenter<BaseView>()
如果使用星形投影,解决方案比我想象的要简单
所以我在 BaseActivity 中替换了
abstract fun getPresenter():BasePresenter<BaseView>
到
abstract fun getPresenter():BasePresenter<*>
然后我可以用新的演示者覆盖它,比如
override abstract fun getPresenter(): BaseConnectionPresenter<*>