我定义了一个实现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
类的Enum
方法和来自RelationshipType
接口的name()
方法都具有相同的签名。这在Java中不是问题,那么为什么它在Kotlin中是一个错误,我如何处理它呢?
发布于 2017-06-14 20:16:50
它是一个
kotlin bug-KT-14115,即使您使enum
类实现了包含name()
函数的接口,也会被拒绝。
interface Name {
fun name(): String;
}
enum class Color : Name;
// ^--- the same error reported
但您可以通过使用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);
发布于 2019-03-20 14:12:56
上面的例子是使用一个具有属性name
而不是函数name()
的接口。
interface Name {
val name: String;
}
enum class Color : Name {
Blue
}
https://stackoverflow.com/questions/44553148
复制相似问题