多个标记位排列组合的优雅写法
2019-11-07 本文已影响0人
hi一一
源码中非常常见的写法;demo如下:
public class JavaMain {
private static int DEFAULT = 0x00000000;//默认
private static int CLICKABLE = 0x00000001;//A 可以点击
private static int LONG_CLICKABLE = 0x00000010;//B 可以长按点击
private static int mTag = DEFAULT;
public static void main(String[] args) {
System.out.println("/////////设置,写////////");
mTag |= CLICKABLE; //将标志位置1
//mTag &= ~CLICKABLE; //初始化或者还原标志位0
mTag |= LONG_CLICKABLE; //将标志位置1
//mTag &= ~LONG_CLICKABLE; //初始化或者还原标志位0
System.out.println("///////读取//////////");
System.out.println((mTag & LONG_CLICKABLE) != 0);//标记位1,0
System.out.println((mTag & CLICKABLE) != 0);
System.out.println(isLongClickable());
}
public static boolean isLongClickable() {
return (mTag & LONG_CLICKABLE) == LONG_CLICKABLE;
}
}