Android @IntDef注解取代Java enum枚举提高性能
为提高Android性能,Android官方建议使用@IntDef注解替代Java的enum枚举。@IntDef的使用给出一个例子,先建立@IntDef数据结构模型,例如MyDay.java:
package zhangphil.test; import android.support.annotation.IntDef; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; public class MyDay { 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({SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}) @Retention(RetentionPolicy.SOURCE) public @interface WeekDays { } @WeekDays private int mCurDay = SUNDAY; public void setCurrentDay(@WeekDays int currentDay) { this.mCurDay = currentDay; } @WeekDays public int getCurrentDay() { return mCurDay; } }
使用:
MyDay days = new MyDay(); days.setCurrentDay(MyDay.TUESDAY); @MyDay.WeekDays int day = days.getCurrentDay(); switch (day) { case MyDay.SUNDAY: break; case MyDay.MONDAY: break; case MyDay.TUESDAY: break; case MyDay.WEDNESDAY: break; case MyDay.THURSDAY: break; case MyDay.FRIDAY: break; case MyDay.SATURDAY: break; default: break; }