在我的系统中,我有一个类,其中加载了(20?)final
booleans
,定义了此用户类型的权限。
有什么更好的方法可以做到这一点?
我确定有很多这方面的例子,但我不知道关键字。
答案 0 :(得分:12)
您可以利用枚举,例如:
public enum Permission {
READ, WRITE;
}
public class User {
private final EnumSet<Permission> permissions;
public User(Permission... permissions) {
this.permissions = EnumSet.copyOf(Arrays.asList(permissions));
}
public boolean hasPermission(Permission permission) {
return permissions.contains(permission);
}
//...
}
User user = new User(Permission.READ, Permission.WRITE);
答案 1 :(得分:8)
创建一个类Permission
,并为每个User
提供一个集合。如果权限在用户的权限集合中,则用户具有该权限,否则不具有权限。
答案 2 :(得分:5)
你可以使用这个掩码
public interface IPersmisions {
public static final int NONE = 1 << 0;
public static final int LEVEL1 = 1 << 1;
public static final int LEVEL2 = 1 << 2;
public static final int LEVEL3 = 1 << 3;
public static final int LEVEL4 = 1 << 4;
public static final int LEVEL5 = 1 << 5;
public static final int LEVEL6 = 1 << 6;
public static final int LEVEL7 = 1 << 7;
public static final int LEVEL8 = 1 << 8;
public static final int LEVEL9 = 1 << 9;
}
使用中你添加
int permisions = IPersmisions.NONE;
public boolean checkPermission(int permission) {
return (permissions & permission) != 0;
}
public void addPermission(int permission) {
permissions = (permissions | permission);
}
public void removePermission(int permission) {
permissions = (permissions & ~permission);
}
比您可以为用户设置一个轻松设置的组
public interface IPermisionGroup extends IPermisions {
public static final int LEVEL1_2_3 = LEVEL1 | LEVEL2 | LEVEL3;
public static final int LEVEL4_5_6 = LEVEL4 | LEVEL5 | LEVEL6;
public static final int LEVEL7_8_9 = LEVEL7 | LEVEL8 | LEVEL9;
}
在您可以使用的代码中就像这样
user.addPermision(IPermisions.LEVEL1);
或
user.addPermision(IPermisions.LEVEL1 | IPermision.LEVEL2);
或
user.addPermision(IPermisionGroup.LEVEL1_2_3);
答案 3 :(得分:4)
@tdammers建议的完整示例:
class enum Permission {
WRITE, READ, EXECUTE, GROUP, OWNER
}
public class User {
protected Set<Permission> permissions;
public User() {
permissions = new EnumSet<Permission>();
}
public boolean hasPermission(Permission p) {
return permissions.contains(p);
}
public void addPermisssion(Permission p) {
permissions.add(p);
}
public void removePermisssion(Permission p) {
permissions.remove(p);
}
}
//usage
User u = new User();
u.addPermission(Permission.READ);
System.out.writeln(u.hasPermission(Permission.WRITE));
u.removePermission(Permission.READ);
答案 4 :(得分:0)
如果您不了解关键字和语法,可能需要阅读有关Java的书籍或在线进行一些教程。但是,一个类看起来像这样:
public class User {
private final boolean admin, superuser, guest;
public User(boolean admin, boolean superuser, boolean guest) {
this.admin = admin;
this.superuser = superuser;
this.guest = guest;
}
public boolean isAdmin() {
return admin;
}
public boolean isSuperuser() {
return superuser;
}
public boolean isGuest() {
return guest;
}
// and so on
}
答案 5 :(得分:-1)
多态性,而不是条件。
class User {
final Print print;
User(Print print) {
this.print = print;
}
}
interface Print {
void print();
}
class CanPrint implements Print {
public void print() { do whatever it takes to print a user }
}
class CannotPrint implements Print {
public void print() { throw null; }
}