在Java中,我可以定义枚举注释类型(从此处开始)
// Constants
public static final String WINTER ="Winter";
public static final String SPRING ="Spring";
public static final String SUMMER ="Summer";
public static final String FALL ="Fall";
// Declare the @ StringDef for these constants:
@StringDef({WINTER, SPRING, SUMMER, FALL})
@Retention(RetentionPolicy.SOURCE)
public @interface Season {}
此代码的Kotlin版本是什么?
使用此工具时出现问题(使用IDE进行直接转换)
// Constants
private const val WINTER ="Winter"
private const val SPRING ="Spring"
private const val SUMMER ="Summer"
private const val FALL ="Fall"
// Declare the @ StringDef for these constants:
@StringDef(WINTER, SPRING, SUMMER, FALL)
@Retention(AnnotationRetention.SOURCE)
annotation class Season
因为我无法访问例如 季节冬天
在Kotlin中,最好使用enum class。 在Kotlin中转换@IntDef和@StringDef用法时遇到很多问题。
enum class Season constructor(val value: String) {
WINTER("Winter"),
SPRING("Spring"),
SUMMER("Summer"),
FALL("Fall");
override fun toString(): String = value
}