Types 与Kotlin和MVP Android一起使用generic
我是Kotlin的新手,我用这种语言启动了一个新的Android项目。从一开始,我就遇到了一些问题,当时a试图使用泛型类型在Kotlin中创建MVP模式的模式。这就是我试图实现的目标: AbstractBasePresenter.ktTypes 与Kotlin和MVP Android一起使用generic,types,required,mismatch,nothing,Types,Required,Mismatch,Nothing,我是Kotlin的新手,我用这种语言启动了一个新的Android项目。从一开始,我就遇到了一些问题,当时a试图使用泛型类型在Kotlin中创建MVP模式的模式。这就是我试图实现的目标: AbstractBasePresenter.kt abstract class AbstractBasePresenter<T: Any> internal constructor( private val compositeDisposable: CompositeDisposable =
abstract class AbstractBasePresenter<T: Any> internal constructor(
private val compositeDisposable: CompositeDisposable = CompositeDisposable()
) {
constructor() : this(CompositeDisposable())
private var view: T? = null
fun bind(view: T) {
if (this.view != null) throw IllegalStateException("view already bound")
this.view = view
afterBind()
}
fun unbind() {
if (this.view == null) throw IllegalStateException("view already unbound")
this.view = null
compositeDisposable.clear()
}
abstract fun afterBind()
protected fun addDisposable(disposable: Disposable): Disposable =
disposable.also {
compositeDisposable.add(it)
}
protected fun view(): T = view ?: throw IllegalStateException("view not bound")
}
interface BaseView<T : AbstractBasePresenter<*>> {
fun getPresenter(): T
}
其想法是,这种类型的实现在Java中运行良好,但在转换之后,Kotlin的泛型类型似乎遗漏了一些东西,在绑定视图时,我在AbstractBaseActivity中遇到了以下错误:
**getPresenter().bind(this)** ------> "this" is underlined and I have the error: **"Type mismatch. Required nothing AbstractBaseActivity<T> found"**
**getPresenter().bind(this)**-->“this”带下划线,我有一个错误:**“类型不匹配。未找到任何必需的AbstractBaseActivity”**
请告诉我哪里做错了,或者给我另一种解决方案来完成实际的BaseActivity范围(从单个位置绑定和解除绑定)。
谢谢大家!
class SplashActivity : AbstractBaseActivity<SplashPresenter>(), SplashActivityView {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_activity)
}
override fun getPresenter(): SplashPresenter {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
class SplashPresenter : AbstractBasePresenter<SplashActivityView>() {
override fun afterBind() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
interface SplashActivityView {
}
**getPresenter().bind(this)** ------> "this" is underlined and I have the error: **"Type mismatch. Required nothing AbstractBaseActivity<T> found"**