我有一个自定义 Exception看起来像这样的类:
class GenericException(message: String?, errorCode: Int) : RuntimeException(message), GraphQLError {
.....
}
众所周知,
RuntimeException extends Throwable它有一个名为
getMessage() 的方法
现在的问题是,这个接口(interface)
GraphQLError (这是一个库接口(interface))还有一个名为
getMessage() 的方法
结果,编译器提示这个:
好的,所以我实现了该方法:
override fun getMessage(): String {
TODO("Not yet implemented")
}
现在我明白了:
我应该在这里做什么?
请您参考如下方法:
我在评论中猜对了,kotlin 允许多重继承。确实是因为 Throwable 类。
您可以使用 @JvmField注解指示编译器不要为字段生成 getter 和 setter,然后自己创建 getter/setter。
interface HasMessage {
fun getMessage(): String
}
class GenericException(
@JvmField override val message: String?, // var is also possible
val errorCode: Int // I made it a property, might not be as well
) : RuntimeException(message), HasMessage {
override fun getMessage(): String {
// return of the super's getter, probably no use because you have field as property in this class
val superGetMessage = super<RuntimeException>.message
TODO("Not yet implemented")
}
}
Play with the code yourself .




