16 Java 枚举
为什么需要枚举
public class VideoStatus {
public static final int Draft = 1; //草稿
public static final int Review = 2; //审核
public static final int Published = 3; //发布
}
void judgeVideoStatus(int status) {
// status 可以为任意值,编译器也不会提出任何警告
}public enum VideoStatus {
Draft, Review, Published
}
void judgeVideoStatus(VideoStatus status) {
// status 有明确类型约束,编译器会检查从而规避潜在问题
}枚举的所有基本用法
自定义扩充枚举
枚举 + 接口
设计模式
单例模式
策略模式
枚举集合类
EnumSet
EnumMap
最后更新于