所以我有一个像这样的BasePresenter类。 BaseMvpView是一个接口
open class BaseMvpPresenter<View : BaseMvpView> {
}
我正在尝试创建一个BaseMvpActivity类,该类使用BaseMvpPresenter参数化,如下所示:
abstract class BaseMvpActivity<T : BaseMvpPresenter> : BaseActivity(), BaseMvpView {
}
但是我收到以下错误:
One type argument expected for class BaseMvpPresenter<View: BaseMvpView>
我如何正确宣布这个?我希望能够像这样使用BaseMvpActivity:
abstract class BaseMvpActivity<T : BaseMvpPresenter<U>, U : BaseMvpView> : BaseActivity(), BaseMvpView {
abstract var presenter: T
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter.onViewCreated(this)
}
override fun onDestroy() {
presenter.onViewDestroyed()
super.onDestroy()
}
}
答案 0 :(得分:4)
您需要将参数传递给BaseMVPPresenter
。您可以向抽象类添加第二个参数,如下所示
abstract class BaseMvpActivity<V: BaseMvpView, T : BaseMvpPresenter<V>> : BaseActivity(), BaseMvpView {
}
所以我们假设你要创建一个显示帖子的视图,然后你会做这样的事情:
class Activity : BaseMVPActivity<PostsView, PostPresenter<PostView>() {
//Here comes your code
}
希望这会有所帮助:)