Android使用@IntDef和枚举enum限制元素取值范围的比较
【摘要】 枚举的作用就是 定义此元素允许的常量
1.使用@IntDef限制元素的取值范围
package com.wong.ex;
import androidx.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
pu...
枚举的作用就是
定义此元素允许的常量
1.使用@IntDef限制元素的取值范围
package com.wong.ex;
import androidx.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
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;
// 定义@WeekDays注解 @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); // 声明变量,限制变量的取值范围 @WeekDays int today = getCurrentDay(); switch (today) { case SUNDAY: break; case MONDAY: break; case TUESDAY: break; case WEDNESDAY: break; case THURSDAY: break; case FRIDAY: break; case SATURDAY: break; default: break; } setCurrentDay(SUNDAY) } // 添加@WeekDays注解,限制传入值的范围 public void setCurrentDay(@WeekDays int currentDay) { this.currentDay = currentDay; }
// 添加@WeekDays注解,限制返回值的范围 @WeekDays public int getCurrentDay() { return currentDay; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
虽然getCurrentDay()方法使用了@WeekDays注解限制它返回的值的范围,但是从实践来看,限制的作用有时会失效,如:
@WeekDays public int getCurrentDay() { return 888; }
- 1
- 2
- 3
- 4
上面的代码@WeekDays起作用了,因为888不是所期望的取值。但是也仅仅是提示而已,告诉你必须是那些上面定义的那些常量。编译依然能通过,程序照样运行。下面这种,干脆就不提示了:
@WeekDays public int getCurrentDay() { return currentDay+888; }
- 1
- 2
- 3
- 4
setCurrentDay()方法也是有同样的问题。
总的来说@IntDef的限制,感觉没有那么严格。
2.使用枚举来限制元素的取值范围
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; private enum WeekDays {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}; WeekDays currentDay = WeekDays.SUNDAY; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //声明变量 WeekDays today = getCurrentDay(); switch (today) { case SUNDAY: break; case MONDAY: break; case TUESDAY: break; case WEDNESDAY: break; case THURSDAY: break; case FRIDAY: break; case SATURDAY: break; default: break; } setCurrentDay(WeekDays.SUNDAY); } public void setCurrentDay(WeekDays currentDay) { this.currentDay = currentDay; } public WeekDays getCurrentDay() { return currentDay; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
使用枚举可以非常有效的限制元素的取值范围。
3.总结
虽然枚举可以很有效的限制元素的取值范围,但是这是以牺牲性能为前提的。安卓开发应避免使用Enum(枚举类),因为相比于静态常量Enum会花费两倍以上的内存。所以呢,如果追求性能的话,建议使用@IntDef来限制元素的取值范围。
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/107211105
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)