android - 想要将方法参数限制为 kotlin 中的特定类型
问题描述
我在 kotlin 中使用通用列表。只是我想限制它应该实现一个接口。当我当时设置其他类的列表值时,我也想应用此限制。
class CustomView<T : CodeName> : ConstraintLayout {
private var mList: List<T> = arrayListOf()
fun <D> updateList(list : List<D>) where D : CodeName{
this.mList = list as List<T>
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val binding = DataBindingUtils.setContentView(this,R.layout.main)
val codeNameList = listOf<CodeName>()
binding.customView.updateView(codeNameList) // This is allowed.
val list = listOf<String>("data","test")
binding.customView.updateList(list) // It's allowing me to update value but Ideally it should not allow me because I am sending List<String> and function's expectation is List<T> where T is CodeName..
}
}
我提到了这个链接。但不明白我该如何解决这个问题。
谢谢你的帮助。
解决方案
推荐阅读
- html - 如何使用 Ionic 在工具栏内溢出图像?
- postgresql - PostgreSQL - 元数据库中的数据库不包含任何表
- assembly - 如何在 Visual Studio Code 上运行汇编语言
- c - C 代码在哪里将内存中的字节编码为 Linux 中的特定字符集?
- r - 如何使用包含不同位数的字符串按列对 data.frame 进行排序?
- sql - 仅汇总并发行
- c++ - 使用 std::function 和模板参数将函数传递给函数
- typescript - 为什么 react-scripts 对可选链接有编译错误?
- javascript - 具有已知属性的打字稿对象
- react-native - 如何使我的堆栈导航高效?