[英]Refactoring from Java to Kotlin, having trouble with Generics
can anyone help with refactoring this class to Kotlin, having trouble with generics conversation. 任何人都可以帮助将此类重构为Kotlin,而在泛型对话方面遇到麻烦。
Model: 模型:
abstract class ViewHolderDataBinder<DM : ViewHolderDataModel,
VH : RecyclerView.ViewHolder>(val viewType: Int) {
abstract fun createViewHolder(parent: ViewGroup): VH
abstract fun bindView(model: DM, holder: VH)
}
Java code that want to convert in to Kotlin: 想要转换成Kotlin的Java代码:
SparseArray<ViewHolderDataBinder> viewHolderDataBinders = new SparseArray<>();
And I want to refactor it in to this 我想将其重构为
val viewHolderDataBinders = SparseArray<ViewHolderDataBinder>()
but Kotlin requires to add type parameters in to ViewHolderDataBinder
. 但是Kotlin需要将类型参数添加到
ViewHolderDataBinder
。
Is there any way to not add type parameter as It was not necessary in Java? 有什么方法可以不添加类型参数,因为在Java中这是没有必要的?
UPDATE 更新
if I use private val viewHolderDataBinders = SparseArray<ViewHolderDataBinder<*, *>>()
如果我使用
private val viewHolderDataBinders = SparseArray<ViewHolderDataBinder<*, *>>()
then getting error here binder.bindView(item, holder)
然后在这里出现错误
binder.bindView(item, holder)
it says that item
and holder
requires Nothing
but Found
..... 它说,
item
和holder
要求Nothing
,但Found
.....
您可以使用此*
符号将任何对象添加到数组中。
val viewHolderDataBinders = SparseArray<ViewHolderDataBinder<*, *>>()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.