Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我可以将可变列表参数传递给在Kotlin中接受列表参数的类?_Kotlin - Fatal编程技术网

为什么我可以将可变列表参数传递给在Kotlin中接受列表参数的类?

为什么我可以将可变列表参数传递给在Kotlin中接受列表参数的类?,kotlin,Kotlin,CustomAdapter类接受代码B中的列表参数,但为什么我可以使用代码a通过传递MutableList参数来创建CustomAdapter的对象 代码A private lateinit var allList: MutableList<MSetting> allList=SettingHandler().getListAllSetting().toMutableList() mCustomAdapter= CustomAdapter(allList) mRecyclerVie

CustomAdapter类接受代码B中的列表参数,但为什么我可以使用代码a通过传递MutableList参数来创建CustomAdapter的对象

代码A

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