Java to Kotlin泛型翻译

时间:2016-12-14 09:53:39

标签: java generics kotlin

我想转移到Kotlin旧的Java项目,并发现有趣,无法将此转换为Kotlin而不会感到痛苦

public interface BaseJView<P extends BaseJPresenter> {
    P createPresenter();
}
public interface BaseJPresenter<V extends BaseJView> {
    void bindView(V view);
}

你能给出建议吗,我怎么能做到这一点?

1 个答案:

答案 0 :(得分:4)

一种方法是像这样使用recursive type definition

interface BaseJView<TSelf : BaseJView<TSelf, P>, P : BaseJPresenter<P, TSelf>> {
    fun createPresenter(): P
}

interface BaseJPresenter<TSelf : BaseJPresenter<TSelf, V>, V : BaseJView<V, TSelf>> {
    fun bindView(view: V)
}

然后你可以:

class Presenter : BaseJPresenter<Presenter, View> {
    override fun bindView(view: View) { ... }
}
class View : BaseJView<View, Presenter> {
    override fun createPresenter(): Presenter { ... }
}