【发布时间】:2018-05-12 23:48:37
【问题描述】:
我尝试在kotlin中创建一个自定义异常,同时也实现了GraphQLError接口,需要getMessage()方法。
如果我尝试实现该方法,我的 IDE 会警告我:
Accidental override: The following declarations have the same JVM signature (getMessage()Ljava/lang/String;):
public open fun <get-message>(): String? defined in eu.mojo.presentation2018.error.ShopException
public open fun getMessage(): String defined in eu.mojo.presentation2018.error.ShopException
但如果我删除它:
Class 'ShopException' is not abstract and does not implement abstract member
public abstract fun getMessage(): String! defined in graphql.GraphQLError
我在网上搜索了解决方案,但都需要对导致冲突的“消息”字段进行一些控制。在我的例子中,这个控件不存在,因为字段消息是在我尝试扩展的异常类中定义的。
这是我的类定义:
class ShopException(code:ErrorCode) : Exception(), GraphQLError {...}
【问题讨论】:
-
你应该能够用返回
String的方法覆盖返回String?的方法,就像你可以用返回@的方法覆盖返回Any的方法一样987654327@ -
出现同样的错误,不管我用的是String还是String?类型...我终于将这个类转换为 .java 并且它工作正常。但是,知道如何(以及是否)可以使用 kotlin 处理这些情况还是很有趣的。
-
我相信这是因为 message 只是接口中的 getter 方法,而是异常类中的 kotlin 属性。尝试从 java.lang.Exception 显式继承,也许可行
-
如果我没记错的话,这就是我已经尝试做的事情: class ShopException(code:ErrorCode) : Exception(), GraphQLError {...} 难道不应该从 java .lang.Exception?
-
你说得对,
kotlin.Exception只是java.lang.Exception的类型别名。GraphQLError是 java 还是 kotlin 类/接口?我从未在 Kotlin 上使用过 GraphQL
标签: inheritance kotlin