【问题标题】:Why having setText() on text field Kotlin compiler compains?为什么在 Kotlin 编译器的文本字段上设置 setText()?
【发布时间】:2020-04-20 07:47:34
【问题描述】:
private class FakeCardDrawable constructor(
    context: Context) : CardDrawable(context) {
    var text: String? = null
    var backgroundColor = 0
    var textColor = 0


    override fun setColors(
        backgroundColor: Int, textColor: Int, animate: Boolean
    ) {
        this.backgroundColor = backgroundColor
        this.textColor = textColor
    }

     fun setText(text: String) {
        this.text = text
    }
}

它抱怨说“平台声明冲突:以下声明具有相同的 JVM 签名(setText..)

【问题讨论】:

标签: android kotlin


【解决方案1】:

因为您的var text 和您的函数setText() 都被转换为JVM 到一个名为setText() 的公共方法。

为避免平台声明冲突,您有 3 个选项:

  1. 更改var text的名称或setText的名称
  2. 将var text 设为私有set:
var text: String? = null
  private set
  1. 更改方法的JVM名称:
@JvmName("myJvmName")
fun setText(text: String) {
    this.text = text
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2011-09-01
    • 2022-09-30
    相关资源
    最近更新 更多