android - 为什么我不能在Kotlin中将Class类型用作通用参数?

标签 android generics kotlin inheritance

我正在使用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)
}

最后,这是我得到的错误:
Error

你能帮我吗?我不知道我在做什么错,谢谢。

最佳答案

这只是一个传递错误的参数。

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/

相关文章:

gradle - Gradle 。 Kotlin-是否可以创建动态(“on fly”)任务?

spring - 如何将spring资源注入(inject)Apache Ignite类?

java - Eclipse 中的 Android 开发无法访问代码 Intent

java - 如何覆盖android中的操作栏后退按钮?

android - 添加类似于 iOS 设备工具栏的底部工具栏

python - 用于通用模型的 Django Createview

Java 自动装箱和拆箱

android - 如何从 PreferenceActivity 外部更改首选项的状态?

c# - 将 'foreach' 与通用列表一起使用时出现问题

javascript - 在函数中返回 postgresql 查询结果