如何克服“相同的 JVM 签名"实现 Java 接口时出错? [英] How to overcome "same JVM signature" error when implementing a Java interface?

查看:26
本文介绍了如何克服“相同的 JVM 签名"实现 Java 接口时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我在 IntelliJ IDEA 13.1.6 和 Kotlin 插件 0.11.91.AndroidStudio.3 中收到以下错误:

With the code below, I am getting the following error in IntelliJ IDEA 13.1.6 and Kotlin plugin 0.11.91.AndroidStudio.3:

Platform declaration clash: The following declarations have the same JVM signature (getName()Ljava/lang/String;):
  • public open fun getName(): kotlin.String?
  • internal final fun <get-name>(): kotlin.String?

Java 类,JavaInterface.java:

public interface JavaInterface {
  public String getName();
}

Kotlin 类,KotlinClass.kt

public class KotlinClass(val name: String?) : JavaInterface

我尝试通过以下方式覆盖getter"方法添加 override fun getName(): String?= name,但会产生相同的错误.

I've tried overriding the 'getter' method by adding override fun getName(): String? = name, but that produces the same error.

通过这样做,我可以看到一种解决方法:

I can see one workaround by doing this instead:

public class KotlinClass(val namePrivate: String?) : JavaInterface {
  override fun getName(): String? = namePrivate
}

但在我的实际案例中,我有许多属性要实现并且也需要 setter.为每个属性执行此操作似乎不太符合 Kotlin 风格.我错过了什么?

But in my real-world case I have a number of properties to implement and need setters too. Doing this for each property doesn't seem very Kotlin-ish. What am I missing?

推荐答案

使该变量 private 解决了问题.

Making that variable private solves the problem.

public class KotlinClass(private val name: String?) : JavaInterface

这篇关于如何克服“相同的 JVM 签名"实现 Java 接口时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆