//避免数据库对同一事物的状态或种类等具有相同类型不同属性的进行多字段控制或表述。例如:性别->男,女;账号权限->增,删,改,查中其中一种或者多种权限。
public static void main(String[] args) {
for(int i = 0;i<129;i++){
//测试
System.out.println(findFlags(i));
}
}
public enum WeekDays
{
Monday(1),//1 ---0000001
Tuesday(1<<1),//2 ---0000010
Wednesday(1<<2),//4 ---0000100
Thursday(1<<3),//8 ---0001000
Friday(1<<4),//16 ---0010000
Saturday(1<<5),//32 ---0100000
Sunday(1<<6);//64 ---1000000
private final int value;
// 构造器默认也只能是private, 从而保证构造函数只能在内部使用
WeekDays(int value) {
this.value = value;
}
}
public static String findFlags(int value) {
if(value < 0){
return String.valueOf(value);
}
String tempStr = "";
for (WeekDays wd : WeekDays.values()) {
if((wd.value & value) == wd.value){
tempStr += wd + ",";
}
}
tempStr = tempStr.equals("") ? String.valueOf(value) : tempStr.substring(0, tempStr.length()-1);
return tempStr;
}
int mFlags = 1<<6;//当前flag
//增加flag
public void addFlags(int flags) {
mFlags |= flags;
}
//删除flag
public void clearFlags(int flags) {
mFlags &= ~flags;
}