问题描述
我定义一个枚举类,它实现Neo4j的 RelationshipType
:
I define an enum class that implements Neo4j's RelationshipType
:
enum class MyRelationshipType : RelationshipType {
// ...
}
I得到以下错误:
继承的平台声明冲突:以下声明具有相同的JVM签名(name()Ljava / lang / String;) :fun< get-name>():String fun name():String
我明白, Enum 类的名称()方法和 name()
RelationshipType
接口具有相同的签名。这不是Java的问题,但是为什么在Kotlin中出现错误,我该如何解决呢?
I understand that both the name()
method from the Enum
class and the name()
method from the RelationshipType
interface have the same signature. This is not a problem in Java though, so why is it an error in Kotlin, and how can I work around it?
推荐答案
它是一个 即使你使枚举
类实现了 name()函数的界面被拒绝。
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 you can simulate a enum
class by using a sealed
class, for example:
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中,当一个枚举类实现一个接口时,如何解决继承的声明冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!