kotlin,Enums,Interface,Kotlin,kotlin" /> kotlin,Enums,Interface,Kotlin,kotlin" />

Enums 在Kotlin中,当枚举类实现接口时,如何解决继承的声明冲突?

Enums 在Kotlin中,当枚举类实现接口时,如何解决继承的声明冲突?,enums,interface,kotlin,kotlin,Enums,Interface,Kotlin,kotlin,我定义了一个实现Neo4j的RelationshipType的枚举类: enum class MyRelationshipType : RelationshipType { // ... } 我得到以下错误: 继承的平台声明冲突:以下声明具有相同的JVM签名(name()Ljava/lang/String;):fun():String fun name():String 我知道Enum类中的name()方法和RelationshipType接口中的name()方法具有相同的签名。但是,

我定义了一个实现Neo4j的
RelationshipType
的枚举类:

enum class MyRelationshipType : RelationshipType {
    // ...
}
我得到以下错误:

继承的平台声明冲突:以下声明具有相同的JVM签名(name()Ljava/lang/String;):fun():String fun name():String

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

即使让
enum
类实现包含
name()
函数的接口,它也会被拒绝

interface Name {
    fun name(): String;
}


enum class Color : Name;
       //   ^--- the same error reported
但是您可以使用
密封的
类来模拟
枚举
类,例如:

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);

上面的示例使用的接口具有属性
name
,而不是函数
name()


虽然我理解下一票,因为它对RelationshipType这样的第三方接口没有帮助,但它是一种在接口中使用枚举名称的方法。
interface Name {
    val name: String;
}

enum class Color : Name {
    Blue
}