簡體   English   中英

為什么我不能在 Kotlin 中使用 Class 類型作為泛型參數?

[英]Why I can't use a Class type as a generic parameter in Kotlin?

我在使用 Kotlin 的 Android 應用程序中工作,但是當我嘗試使用具體類作為第四個參數時,出現錯誤,我的問題是,我做錯了什么?

這是 RecyclerView 的基本適配器

abstract class BaseAdapter<K, T: DbEntity<K>, VDB: ViewDataBinding, VH: BaseViewHolder<K,DbEntity<K>, VDB>>: RecyclerView.Adapter<VH>(){
    val items: MutableList<T> = ArrayList()

    fun addNewItems(newItems: List<T>){

    }
}

這是我用於指定泛型參數的類,但出現錯誤

class CaseByCountryViewHolder(mDataBinding: ItemCaseByCountryBinding): BaseViewHolder<Int, CaseByCountry, ItemCaseByCountryBinding>(mDataBinding) {

    override fun bind(item: CaseByCountry) {
    }
}

這是基礎 ViewHolder 類:

abstract class BaseViewHolder<K, T: DbEntity<K>, VDB: ViewDataBinding>(mDataBinding: ViewDataBinding)
    :RecyclerView.ViewHolder(mDataBinding.root){
    protected val context: Context = mDataBinding.root.context
    protected val layoutInflater: LayoutInflater = LayoutInflater.from(context)
    abstract fun bind(item: T)
}

最后,這是我得到的錯誤: 錯誤

你能幫我嗎? 我不知道我做錯了什么,提前致謝。

這只是一個參數傳遞錯誤。

abstract class BaseAdapter<K, t : A<K>, 
VDB: ViewDataBinding, VH: BaseViewHolder<K,t, VDB>>{ }

class CaseByCountryAdapter() 
: BaseAdapter<Int, CaseByCountry, ViewDataBinding, CaseByCountryViewHolder>()

abstract class BaseViewHolder<K, t : A<K>, VDB: ViewDataBinding> {}

class CaseByCountryViewHolder(mDataBinding: ItemCaseByCountryBinding)
: BaseViewHolder<Int, CaseByCountry, ViewDataBinding>() {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM