优点:位运算减少空间,但是会减少代码的可读性
1.公用类设计
public class Auth { public static int insertAuth = 1 << 0;//增权限 public static int updateAuth = 1 << 1;//修改权限 public static int queryAuth = 1 << 2;//查询权限 public static int deleteAuth = 1 << 3;//删除权限 public static int currentAuth; //初始化权限 public static void initAuth(int value) { currentAuth = currentAuth | value; } //添加权限 public static void addAuth(int value) { currentAuth = currentAuth | value; } //删除权限 public static void delAuth(int value) { currentAuth &= ~value; } //判断是否有权限 public static boolean isAuth(int value) { return (currentAuth | value) == currentAuth; } }
2.权限类调用
int value=Auth.insertAuth|Auth.deleteAuth; initAuth(value); System.out.println(Auth.isAuth(Auth.insertAuth)); delAuth(value); System.out.println(Auth.isAuth(Auth.insertAuth));