java - Kotlin 中的每个类都扩展任何类吗
问题描述
我正在编写一个扩展函数RecyclerView
来设置LayoutManager
和Adapter
设置RecyclerView
。该函数应该接受任何扩展自的适配器类,RecyclerView.Adapter<Any>
就像我们在 Java 中使用的一样。我们RecyclerView.Adapter<?>
如何在 Kotlin 中实现这一点?
internal fun <T : RecyclerView.Adapter<Any>> RecyclerView.initAndSetAdapter(context: Context, adapter: T) {
this.layoutManager = LinearLayoutManager(context)
this.setHasFixedSize(true)
this.adapter = adapter
}
由于类型无效,对此方法的函数调用未编译。但如果我更改Any
为,*
则它可以正常工作。
internal fun <T : RecyclerView.Adapter<*>> RecyclerView.initAndSetAdapter(context: Context, adapter: T) {
this.layoutManager = LinearLayoutManager(context)
this.setHasFixedSize(true)
this.adapter = adapter
}
为什么方法调用不与Any
类一起编译?Kotlin 中的每个类都Any
像 JavaObject
类一样扩展类吗?
解决方案
根据文档: https ://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html
Every Kotlin class has Any as a superclass.
话虽如此 - 适配器需要的类需要从 ViewHolder 扩展。
推荐阅读
- api - 持久性框架
- google-bigquery - 在 Native Bigquery Zeppelin Interpreter 中获取读取超时
- latex - 有些页面没有为我编号 - let\clearpage\relax
- javascript - 使用 jquery 确认时不会触发文件上传
- mongodb - WritableServerSelector 从集群描述中没有选择服务器
- rust - 在这个 Docopt 示例中,类型推导是如何工作的?
- c# - Visual Studio 2017 中缺少 Windows 安装程序
- node.js - 如何进行 MongoDB 查询并在部分视图中使用它
- c# - 如何为弹性条件源描述符创建 Nest
- r - R 正则表达式问题:“-\\s[AZ]”小写匹配的大写字母并删除破折号+空格