【发布时间】:2017-01-06 17:19:03
【问题描述】:
我想要实现的是我在 Java 中使用的一个简单模式,并且根据 documentation 在 Kotlin 中应该是可行的。我只想声明一个枚举类,其中包含几个实现相同抽象函数的常量定义。
我的问题是我无法编译我的代码。我总是遇到同样的错误:
modifier abstract not allowed here
代码如下:
enum class Program {
HOME {
override fun readableName(context: Context): String {
return context.getString(R.string.program_home)
}
},
WEEKEND {
override fun readableName(context: Context): String {
return context.getString(R.string.program_weekend)
}
},
SHOPPING {
override fun readableName(context: Context): String {
return context.getString(R.string.program_shopping)
}
};
abstract fun readableName(context: Context): String
}
我什至尝试过使用文档中的示例代码,但即使这样也无法编译。
有人知道这个奇怪的问题吗?顺便说一句,我目前正在使用 Kotlin 1.0.6。
【问题讨论】:
-
"枚举匿名类" - 它的哪一部分是匿名的?也许您的意思是“如何在 Kotlin 中将 Enum 与抽象函数一起使用”或类似的东西?
-
@voddan 基于这个问题,我会说你是对的。我已经编辑了标题。
标签: android enums kotlin apt kapt