Android @IntDef注解取代Java enum枚举提高性能
为提高Android性能,[email protected]@IntDef的使用给出一个例子,[email protected],例如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; }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/3096.html