到目前为止,我一直在使用Kotlin密封类:
sealed class ScanAction {
class Continue: ScanAction()
class Stop: ScanAction()
... /* There's more but that's not super important */
}
在我的Kotlin和Java代码方面都做得很好。今天,我尝试将此类改为使用对象(建议减少类的额外实例化):
sealed class ScanAction {
object Continue: ScanAction()
object Stop: ScanAction()
}
我可以在其他Kotlin文件中引用这种简单的方法,但是现在我正努力在Java文件中使用它。
在尝试引用Java时,我尝试了以下方法,并且这两种方法都会反汇编错误:
ScanAction test = ScanAction.Continue;
ScanAction test = new ScanAction.Continue();
有人知道我现在如何引用Java中的实例吗?
答案 0 :(得分:8)
您必须使用INSTANCE
属性:
ScanAction test = ScanAction.Continue.INSTANCE;