Kotlin中的泛型和抽象类

时间:2017-02-17 08:29:11

标签: java android kotlin

我有一个基础抽象类:

abstract class BaseFragment<T : BasePresenter> : Fragment(){

    protected var presenter : T? = null

    abstract fun providePresenter() : T

    abstract fun getLayoutId() : Int

    abstract fun onCreate()

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val root = inflater?.inflate(getLayoutId(), container, false)
        presenter = providePresenter()
        return root
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        onCreate()
    }

}

我在JAVA中的代码:

//JAVA

private ArrayList<BaseFragment> fragments;

我怎样才能使用Kotlin?代码吼叫不工作

//Kotlin

val tabs = ArrayList<BaseFragment>() //error: One type argument expected for class BaseFragment<T : BasePresenter> : Fragment

1 个答案:

答案 0 :(得分:2)

您需要为T

指定类型
val tabs = ArrayList<BaseFragment<SomePresenterType>>()