我定义了一个enum类来实现Neo4j的RelationshipType:

enum class MyRelationshipType : RelationshipType {
    // ...
}

我得到以下错误:

Inherited platform declarations clash: The following declarations have the same JVM signature (name()Ljava/lang/String;): fun <get-name>(): String fun name(): String

我知道Enum类的name()方法和RelationshipType接口的name()方法都有相同的签名.但这在Java中不是问题,所以为什么Kotlin中会出现错误,我该如何解决这个问题呢?

推荐答案

it is a bug-KT-14115 even if you makes the enum class implements the interface which contains a name() function is denied.

interface Name {
    fun name(): String;
}


enum class Color : Name;
       //   ^--- the same error reported

BUT您可以使用sealed类来模拟enum类,例如:

interface Name {
    fun name(): String;
}


sealed class Color(val ordinal: Int) : Name {
    fun ordinal()=ordinal;
    override fun name(): String {
        return this.javaClass.simpleName;
    }
    //todo: simulate other methods ...
};

object RED : Color(0);
object GREEN : Color(1);
object BLUE : Color(2);

Kotlin相关问答推荐

Kotlin—从列表中枚举属性计算不同值的数量

两个LocalDateTime之间的Kotlin差异

在 Kotlin 中,为什么我们要在闭包中捕获值

为什么使用 return instance ?: synchronized(this) { instance ?: PreferenceParameterState(context) } 时无法获得单例?

为什么 Kotlin main 函数需要 @JVMStatic 注解?

mutableStateOf 和 mutableStateListOf 有什么区别?

如果不在可组合函数中,如何获取 stringResource

验证构造函数中的值组合

PRDownloader 即使在实现库后也无法工作.未知参考:Android Studio 中的 PRDownloader

是什么让 Kotlin 中的 String 类能够使用方括号?

Kotlin 可打包类抛出 ClassNotFoundException

Dagger 2 ContributesAndroidInjector 为模块提供活动

`this@classname` 在 Kotlin 中是什么意思?

将协同路由调用放在存储库或ViewModel中哪个更好?

在kotlin中,如何模拟封装回调函数?

Lint 错误:可疑的相等判断:在 Object DiffUtilEquals 中未实现 equals()

kotlin中的全局可拓函数

lateinit 的 isInitialized 属性在伴随对象中不起作用

如何在kotlin用mockito模仿lambda

java.lang.NoClassDefFoundError:解析失败:Lkotlin/time/MonoClock