在Java中,被 enum
关键字修饰的类型就是枚举类型
public enum TypeEnum {
TYPE_IMAGE,
TYPE_VIDEO,
}
如果枚举不添加任何方法,枚举值默认为从0开始的有序数值。以 TypeEnum 枚举类型举例,它的枚举常量依次为
TYPE_IMAGE:0,TYPE_VIDEO:1
所以刚才的代码等价于调用了两次Enum(String name, int ordinal))
,即
new Enum<TypeEnum>("TYPE_IMAGE", 0);
new Enum<TypeEnum>("TYPE_VIDEO", 1);
for(TypeEnum t:TypeEnum.values()){
Log.d("TypeEnum",""+t+":"+t.ordinal()+","+t.name());
}
使用枚举作为switch语句判断条件能让我们的代码可读性更强:
TypeEnum type = TypeEnum.TYPE_IMAGE;
switch (type) {
case TYPE_IMAGE:
Log.d("TypeEnum", "图片类型");
break;
case TYPE_VIDEO:
Log.d("TypeEnum", "视频类型");
break;
default:
Log.d("TypeEnum", "类型错误");
break;
}
运行结果
枚举类型的构造函数是私有的 private,所以不能通过 New 来创建枚举类型,正因如此才保证了枚举数据类型的安全,不会被更改
public enum Color {
RED("red",1),
YELLOW("yellow",2),
BLUE("blue",3);
private String name;
private int index;
Color(String name,int index){
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public int getIndex() {
return index;
}
}
Color color = Color.RED;
switch (color) {
case RED:
Log.d("Color", "红色象征热情"+color.getIndex());
break;
case YELLOW:
Log.d("Color", "黄色象征活力"+color.getIndex());
break;
case BLUE:
Log.d("Color", "蓝色象征深沉"+color.getIndex());
break;
default:
Log.d("Color", "不存在的颜色");
break;
}
再举个栗子
public enum LanguageEnum {
LANGUAGE("language"),
LANGUAGE_zh("zh"),
LANGUAGE_en("en");
private String language;//自定义属性
/**构造函数,枚举类型只能为私有*/
LanguageEnum(String language) {
this.language = language;
}
public String getLanguage(){
return language;
}
}
Log.d("TTT","语言类型为"+LanguageEnum.LANGUAGE_zh.getLanguage());
日志输出
语言类型为zh
大佬们说枚举肥肠占用内存
其实Android为我们提供了枚举的替代方案 —— 注解方式(@IntDef、@StringDef、@LongDef),其性能远远高于枚举,下面我们就来说说如何使用
首先添加依赖
implementation 'com.android.support:support-annotations:28.0.0'
public class MainActivity extends AppCompatActivity {
//先定义 常量
public static final int SUNDAY = 0;
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
//用 @IntDef "包住" 常量;
// @Retention 定义策略
// 声明构造器
@IntDef({
SUNDAY, MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY})
@Retention(RetentionPolicy.SOURCE)
public @interface WeekDays {
}
@WeekDays int currentDay = SUNDAY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDay(WEDNESDAY);
//声明变量
@WeekDays int today = getCurrentDay();
switch (today){
case SUNDAY:
Log.d("TTT","SUNDAY");
break;
case MONDAY:
Log.d("TTT","MONDAY");
break;
case TUESDAY:
Log.d("TTT","TUESDAY");
break;
case WEDNESDAY:
Log.d("TTT","WEDNESDAY");
break;
case THURSDAY:
Log.d("TTT","THURSDAY");
break;
case FRIDAY:
Log.d("TTT","FRIDAY");
break;
case SATURDAY:
Log.d("TTT","SATURDAY");
break;
default:
break;
}
}
public void setCurrentDay(@WeekDays int currentDay) {
this.currentDay = currentDay;
}
@WeekDays
public int getCurrentDay() {
return currentDay;
}
}
我们可以换种写法,差不多,只不过是在初始化注解类的时候给成员赋值
public class SecondActivity extends AppCompatActivity {
//注解类的成员默认就是 public static final 修饰的
@IntDef({
WeekDays.SUNDAY, WeekDays.MONDAY, WeekDays.TUESDAY, WeekDays.WEDNESDAY, WeekDays.THURSDAY, WeekDays.FRIDAY, WeekDays.SATURDAY})
@Retention(RetentionPolicy.SOURCE)
public @interface WeekDays {
int SUNDAY = 0;
int MONDAY = 1;
int TUESDAY = 2;
int WEDNESDAY = 3;
int THURSDAY = 4;
int FRIDAY = 5;
int SATURDAY = 6;
}
@WeekDays
int currentDay = WeekDays.SUNDAY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setCurrentDay(WeekDays.WEDNESDAY);
int today = getCurrentDay();
switch (today) {
case WeekDays.SUNDAY:
Log.d("TTT", "SUNDAY");
break;
case WeekDays.MONDAY:
Log.d("TTT", "MONDAY");
break;
case WeekDays.TUESDAY:
Log.d("TTT", "TUESDAY");
break;
case WeekDays.WEDNESDAY:
Log.d("TTT", "WEDNESDAY");
break;
case WeekDays.THURSDAY:
Log.d("TTT", "THURSDAY");
break;
case WeekDays.FRIDAY:
Log.d("TTT", "FRIDAY");
break;
case WeekDays.SATURDAY:
Log.d("TTT", "SATURDAY");
break;
}
}
public void setCurrentDay(@WeekDays int currentDay) {
this.currentDay = currentDay;
}
@WeekDays
public int getCurrentDay() {
return currentDay;
}
}
@IntDef
用于简化从枚举到整型值的转换,实现 String 类型的效果时需用@StringDef
,还有 @LongDef
Java枚举:小小enum,优雅而干净