为什么在文本字段Kotlin编译器compains上使用setText()


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..(

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

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

  1. 更改var text的名称或setText的名称
  2. 使用专用set制作var text
var text: String? = null
private set
  1. 更改方法的JVM名称:
@JvmName("myJvmName")
fun setText(text: String) {
this.text = text
}

最新更新