class Admin {
private String aid ;
private String password ;
private Role role ;
public Admin(String aid,String password) {
this.aid =aid ;
this.password = password ;
}
public void setRole(Role role) {
this.role = role ;
}
public Role getRole() {
return this.role ;
}
public String getInfo() {
return "管理员编号:" + this.aid + ",密码:" + this.password ;
}
}
class Role {
private int rid ;
private String title ;
private Admin admins [] ;
private Group groups [] ;
public Role(int rid,String title) {
this.rid = rid ;
this.title = title ;
}
public void setAdmins(Admin [] admins) {
this.admins = admins ;
}
public void setGroups(Group groups[]) {
this.groups = groups ;
}
public Group [] getGroups() {
return this.groups ;
}
public Admin [] getAdmins() {
return this.admins ;
}
public String getInfo() {
return "角色编号:" + this.rid + ",名称:" + this.title ;
}
}
class Group {
private int gid ;
private String title ;
private Role roles [] ;
private Action actions [] ;
public Group(int gid,String title) {
this.gid = gid ;
this.title = title ;
}
public void setRoles(Role roles[]) {
this.roles = roles ;
}
public void setActions(Action actions[]) {
this.actions = actions ;
}
public Action [] getActions() {
return this.actions ;
}
public Role [] getRoles() {
return this.roles ;
}
public String getInfo() {
return "权限组编号:" + this.gid + ",名称:" + this.title ;
}
}
class Action {
private int aid ;
private String title ;
private String url ;
private Group group ;
public Action(int aid,String title,String url) {
this.aid = aid ;
this.title = title ;
this.url = url ;
}
public void setGroup(Group group) {
this.group = group ;
}
public Group getGroup() {
return this.group ;
}
public String getInfo() {
return "权限编号:" + this.aid + ",名称:" + this.title + ",路径:" + this.url ;
}
}
public class TestAdmin {
public static void main (String args[]) {
//第一步,设置完整关系
//1、准备出若干个对象
Admin a1 = new Admin("admin","hello") ;
Admin a2 = new Admin("guest","hello") ;
Admin a3 = new Admin("jiang","hello") ;
Role r1 = new Role(1,"系统管理员") ;
Role r2 = new Role(2,"信息管理员") ;
Group g1 = new Group(10,"信息管理") ;
Group g2 = new Group(11,"用户管理") ;
Group g3 = new Group(12,"数据管理") ;
Group g4 = new Group(13,"接口管理") ;
Group g5 = new Group(14,"备份管理") ;
Action at01 = new Action(1001,"新闻发布","-") ;
Action at02 = new Action(1002,"新闻列表","-") ;
Action at03 = new Action(1003,"新闻审核","-") ;
Action at04 = new Action(1004,"增加用户","-") ;
Action at05 = new Action(1005,"用户列表","-") ;
Action at06 = new Action(1006,"登录日志","-") ;
Action at07 = new Action(1007,"雇员数据","-") ;
Action at08 = new Action(1008,"部门数据","-") ;
Action at09 = new Action(1009,"公司数据","-") ;
Action at10 = new Action(1010,"服务传输","-") ;
Action at11 = new Action(1011,"短信平台","-") ;
Action at12 = new Action(1012,"全部备份","-") ;
Action at13 = new Action(1013,"局部备份","-") ;
//2、设置对象之间的基本关系
//设置管理员与角色
a1.setRole(r1) ;
a2.setRole(r2) ;
a3.setRole(r2) ;
r1.setAdmins(new Admin[] {a1}) ;
r2.setAdmins(new Admin[] {a2,a3}) ;
//设置角色与管理员组
r1.setGroups(new Group[] {g1,g2,g3,g4,g5}) ;
r2.setGroups(new Group[] {g1,g2}) ;
g1.setRoles(new Role[] {r1,r2}) ;
g2.setRoles(new Role[] {r1,r2}) ;
g3.setRoles(new Role[] {r1}) ;
g4.setRoles(new Role[] {r1}) ;
g5.setRoles(new Role[] {r1}) ;
//设置管理员组与权限
g1.setActions(new Action[] {at01,at02,at03}) ;
g2.setActions(new Action[] {at04,at05,at06}) ;
g3.setActions(new Action[] {at07,at08,at09}) ;
g4.setActions(new Action[] {at10,at11}) ;
g5.setActions(new Action[] {at12,at13}) ;
at01.setGroup(g1) ;
at02.setGroup(g1) ;
at03.setGroup(g1) ;
at04.setGroup(g2) ;
at05.setGroup(g2) ;
at06.setGroup(g2) ;
at07.setGroup(g3) ;
at08.setGroup(g3) ;
at09.setGroup(g3) ;
at10.setGroup(g4) ;
at11.setGroup(g4) ;
at12.setGroup(g5) ;
at13.setGroup(g5) ;
//第二步:取出数据内容
System.out.println(a1.getInfo()) ;
System.out.println("\t|-" + a1.getRole().getInfo()) ;
for (int x = 0 ;x < a1.getRole().getGroups().length ;x ++ ){
System.out.println("\t|-" + a1.getRole().getGroups()[x].getInfo()) ;
for (int y = 0 ;y< a1.getRole().getGroups()[x].getActions().length ;y ++ ){
System.out.println("\t\t\t|-" + a1.getRole().getGroups()[x].getActions()[y].getInfo()) ;
}
}
System.out.println("-----------------------------------------------------------------") ;
System.out.println(g2.getInfo()) ;
for (int x = 0 ;x < g2.getRoles().length ; x ++ ){
System.out.println("\t|-" + g2.getRoles()[x].getInfo()) ;
for (int y = 0 ;y < g2.getRoles()[x].getAdmins().length ;y ++ ){
System.out.println("\t\t|-" + g2.getRoles()[x].getAdmins()[y].getInfo()) ;
}
}
}
}
这次使用了win7 自带的PowerShell,运行结果如下