我正在使用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) {
}
}
这是Base 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>() {}
关于android - 为什么我不能在Kotlin中将Class类型用作通用参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60765326/