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

查看:539
本文介绍了如何克服“相同的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();
}

科特琳课,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
}

但是在我的现实世界中,我有许多要实现的属性,也需要设置器.对每个属性执行此操作似乎不是科特林式的.我想念什么?

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天全站免登陆