kotlin - 如何更改构造函数 JVM 签名以防止 Kotlin 中的平台声明冲突
问题描述
我正在尝试使用模型数据制作可扩展的回收器视图。但我收到签名错误。尝试了不同的解决方案但没有奏效。如何将 JvmName 设置为构造函数?
错误: 平台声明冲突:以下声明具有相同的 JVM 签名 ((ILjava/util/List;Z)V):
行模型类:
class RowModel {
companion object{
@IntDef(WEB_MENU, CHILD, CHILDX, CHILDXX)
@Retention(AnnotationRetention.SOURCE)
annotation class RowType
const val WEB_MENU = 1
const val CHILD = 2
const val CHILDX = 3
const val CHILDXX = 4
}
@RowType var type : Int
lateinit var webMenuItem : List<WebMenuItem>
lateinit var child: List<Child>
lateinit var childX: List<ChildX>
lateinit var childXX: List<ChildXX>
var isExpanded : Boolean
constructor (@RowType type : Int, webMenuItem: List<WebMenuItem>, isExpanded : Boolean = false){
this.type = type
this.webMenuItem = webMenuItem
this.isExpanded = isExpanded
}
constructor(@RowType type : Int, child: List<Child>, isExpanded : Boolean = false){
this.type = type
this.child = child
this.isExpanded = isExpanded
}
constructor(@RowType type : Int, childX: List<ChildX>, isExpanded : Boolean = false){
this.type = type
this.childX = childX
this.isExpanded = isExpanded
}
constructor(@RowType type : Int, childXX: List<ChildXX>, isExpanded : Boolean = false){
this.type = type
this.childXX = childXX
this.isExpanded = isExpanded
}
}
webMenuItem 类:
class WebMenuItem {
val authFunctionTag: String = ""
val callFunctionName: Any = ""
val childs: MutableList<Child> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val queryServiceName: Any = ""
val queryTableName: Any = ""
val queryUniqColumnName: Any = ""
val queryUniqFieldName: Any = ""
val menuOrder: Int = ""
}
儿童班:
class Child{
val authFunctionTag: String = ""
val callFunctionName: String = ""
var childs: List<ChildX> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val menuOrder: Int = Int.MIN_VALUE
val queryServiceName: String = ""
val queryTableName: String = ""
val queryUniqColumnName: String = ""
val queryUniqFieldName: String = ""
}
ChildX 类:
class ChildX{
val authFunctionTag: String = ""
val callFunctionName: String = ""
var childs: List<ChildXX> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val menuOrder: Int = Int.MIN_VALUE
val queryServiceName: String = ""
val queryTableName: String = ""
val queryUniqColumnName: String = ""
val queryUniqFieldName: String = ""
}
ChildXX 类:
class ChildXX{
val authFunctionTag: String = ""
val callFunctionName: String = ""
var childs: List<Any> = mutableListOf()
val id: Int = Int.MIN_VALUE
val isQueryWindow: Boolean = false
val menuIcon: String = ""
val menuName: String = ""
val menuOrder: Int = Int.MIN_VALUE
val queryServiceName: String = ""
val queryTableName: String = ""
val queryUniqColumnName: String = ""
val queryUniqFieldName: String = ""
}
解决方案
Kotlin(如 Java)具有类型 erasure。编译后带有类型的参数List<Child>
将被删除List<*>
,因此所有构造函数重载在编译时都具有相同的 JVM 签名。
您可以在文件的顶层或伴随对象中使用具有不同名称的工厂函数来解决这个问题,例如RowModel.withWebMenuItem(...)
,RowModel.withChild(...)
并让实际的构造函数RowModel
成为它们的共同点:
constructor(@RowType type : Int, isExpanded : Boolean = false){
this.type = type
this.isExpanded = isExpanded
}
推荐阅读
- angular - 我们可以在包含庞大数据库的远程服务器中使用 Redis 缓存吗?
- php - 基于自定义日期字段和时间字段的 Wordpress 查询
- spring - saml2Login method cannot be resolved
- algorithm - 使用迭代方法解决这种递归关系的步骤是什么?
- javascript - 如何在 typescript 中声明重新导出的纯 js 函数的类型?
- reactjs - 即使浏览器内存和会话过期,如何保持来自 localStorage 的 blobURL 有用?
- bash - 如何将大输出存储在bash脚本的变量中
- python - 获取调用中的 aiohttp ClientSession 错误:看到的标头字节太多;返回检测到的溢出
- python - Flask SSE 无法连接到事件流。Redis 正在运行吗?
- javascript - 将元素添加到页面而不重新渲染已添加的元素