为什么我可以将可变列表参数传递给在Kotlin中接受列表参数的类?
CustomAdapter类接受代码B中的列表参数,但为什么我可以使用代码a通过传递MutableList参数来创建CustomAdapter的对象 代码A为什么我可以将可变列表参数传递给在Kotlin中接受列表参数的类?,kotlin,Kotlin,CustomAdapter类接受代码B中的列表参数,但为什么我可以使用代码a通过传递MutableList参数来创建CustomAdapter的对象 代码A private lateinit var allList: MutableList<MSetting> allList=SettingHandler().getListAllSetting().toMutableList() mCustomAdapter= CustomAdapter(allList) mRecyclerVie
private lateinit var allList: MutableList<MSetting>
allList=SettingHandler().getListAllSetting().toMutableList()
mCustomAdapter= CustomAdapter(allList)
mRecyclerView.adapter= mCustomAdapter
private lateinit var allList:MutableList
allList=SettingHandler().getListAllSetting().toMutableList()
mCustomAdapter=CustomAdapter(所有列表)
mRecyclerView.adapter=mCustomAdapter
代码B
class CustomAdapter (val backupItemList: List<MSetting>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
}
class CustomAdapter(val backupItemList:List):RecyclerView.Adapter(){
}
如果您查看可变列表
的源代码,您将看到可变列表
扩展了列表
,这就是为什么您可以将可变列表
传递给需要列表
的函数的原因:
public interface MutableList<E> : List<E>
公共接口可变列表:列表
MutableList
只是向List
类型添加了新的功能,使其具有易变性。您应该已经了解了Java多态性,它也适用于Kotlin