【问题标题】:why my class constructor is considered have the same declaration even though the data type is different?为什么即使数据类型不同,我的类构造函数也被认为具有相同的声明?
【发布时间】:2020-08-24 07:32:40
【问题描述】:

所以我有这样的NotificationKM

class NotificationKM() : Parcelable {

    var notificationID : String = ""
    var creatorID : String = ""
    var creatorName : String = ""
    var title : String = ""
    var body : String = ""
    var createdAt : Date = Calendar.getInstance().time
    var imagePath : String = ""
    var type : String = ""
    var hasBeenRead: Boolean = false


    constructor(dataMessage: MutableMap<String,Any>?) : this() {
        

    }

    constructor(data: MutableMap<String!,String!>) : this() {

    }

}

但我有一个错误:

平台声明冲突:以下声明具有相同的 JVM签名

数据类型不同,但我有这个错误。我该怎么办?

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    因为 Java 中的泛型(以及 Kotlin 中的泛型,因为 Kotlin 生成 jvm 字节码)正在使用类型擦除机制,因此,当您的应用程序将被编译时,它将只有 Map 没有项目类型信息。此外,可空性不会反映在字节码上。

    我建议创建两个不同名称的工厂方法:例如createFromDatacreateFromMessage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多