美文网首页
多个标记位排列组合的优雅写法

多个标记位排列组合的优雅写法

作者: hi一一 | 来源:发表于2019-11-07 15:34 被阅读0次

    源码中非常常见的写法;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;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:多个标记位排列组合的优雅写法

          本文链接:https://www.haomeiwen.com/subject/lkjvbctx.html