Java简单类——多对多映射

  1 class Admin {
  2     private String aid ;
  3     private String password ;
  4     private Role role ;
  5     public Admin(String aid,String password) {
  6         this.aid =aid ;
  7         this.password = password ;
  8     }
  9     public void setRole(Role role) {
 10         this.role = role ;
 11     }
 12     public Role getRole() {
 13         return this.role ;
 14     }
 15     public String getInfo() {
 16         return "管理员编号:" + this.aid + ",密码:" + this.password ;
 17     }
 18 }
 19 class Role {
 20     private int rid ;
 21     private String title ;
 22     private Admin admins [] ;
 23     private Group groups [] ;
 24     public Role(int rid,String title) {
 25         this.rid = rid ;
 26         this.title = title ;
 27     }
 28     public void setAdmins(Admin [] admins) {
 29         this.admins = admins ;
 30     }
 31     public void setGroups(Group  groups[]) {
 32         this.groups = groups ;
 33     }
 34     public Group [] getGroups() {
 35         return this.groups ;
 36     }
 37     public Admin [] getAdmins() {
 38         return this.admins ;
 39     }
 40     public String getInfo() {
 41         return "角色编号:" + this.rid + ",名称:" + this.title ;
 42     }
 43 }
 44 class Group {
 45     private int gid ;
 46     private String title ;
 47     private Role roles [] ;
 48     private Action actions [] ;
 49     public Group(int gid,String title) {
 50         this.gid = gid ;
 51         this.title = title ;
 52     }
 53     public void setRoles(Role roles[]) {
 54         this.roles = roles ;
 55     }
 56     public void setActions(Action actions[]) {
 57         this.actions = actions ;
 58     }
 59     public Action [] getActions() {
 60         return this.actions ;
 61     }
 62     public Role [] getRoles() {
 63         return this.roles ;
 64     }
 65     public String getInfo() {
 66         return "权限组编号:" + this.gid + ",名称:" + this.title ; 
 67     }
 68 }
 69 class Action {
 70     private int aid ;
 71     private String title ;
 72     private String url ;
 73     private Group group ;
 74     public Action(int aid,String title,String url) {
 75         this.aid = aid ;
 76         this.title = title ;
 77         this.url = url ;
 78     }
 79     public void setGroup(Group group) {
 80         this.group = group ;
 81     }
 82     public Group getGroup() {
 83         return this.group ;
 84     }
 85     public String getInfo() {
 86         return "权限编号:" + this.aid + ",名称:" + this.title + ",路径:" + this.url ;
 87     }
 88 }
 89 public class TestAdmin {
 90     public static void main (String args[]) {
 91         //第一步,设置完整关系
 92         //1、准备出若干个对象
 93         Admin a1 = new Admin("admin","hello") ;
 94         Admin a2 = new Admin("guest","hello") ;
 95         Admin a3 = new Admin("jiang","hello") ;
 96         Role r1 = new Role(1,"系统管理员") ;
 97         Role r2 = new Role(2,"信息管理员") ;
 98         Group g1 = new Group(10,"信息管理") ;
 99         Group g2 = new Group(11,"用户管理") ;
100         Group g3 = new Group(12,"数据管理") ;
101         Group g4 = new Group(13,"接口管理") ;
102         Group g5 = new Group(14,"备份管理") ;
103         Action at01 = new Action(1001,"新闻发布","-") ;
104         Action at02 = new Action(1002,"新闻列表","-") ;
105         Action at03 = new Action(1003,"新闻审核","-") ;
106         Action at04 = new Action(1004,"增加用户","-") ;
107         Action at05 = new Action(1005,"用户列表","-") ;
108         Action at06 = new Action(1006,"登录日志","-") ;
109         Action at07 = new Action(1007,"雇员数据","-") ;
110         Action at08 = new Action(1008,"部门数据","-") ;
111         Action at09 = new Action(1009,"公司数据","-") ;
112         Action at10 = new Action(1010,"服务传输","-") ;
113         Action at11 = new Action(1011,"短信平台","-") ;
114         Action at12 = new Action(1012,"全部备份","-") ;
115         Action at13 = new Action(1013,"局部备份","-") ;
116         //2、设置对象之间的基本关系
117         //设置管理员与角色
118         a1.setRole(r1) ;
119         a2.setRole(r2) ;
120         a3.setRole(r2) ;
121         r1.setAdmins(new Admin[] {a1}) ; 
122         r2.setAdmins(new Admin[] {a2,a3}) ;
123         //设置角色与管理员组
124         r1.setGroups(new Group[] {g1,g2,g3,g4,g5}) ;
125         r2.setGroups(new Group[] {g1,g2}) ;
126         g1.setRoles(new Role[] {r1,r2}) ;
127         g2.setRoles(new Role[] {r1,r2}) ;
128         g3.setRoles(new Role[] {r1}) ;
129         g4.setRoles(new Role[] {r1}) ;
130         g5.setRoles(new Role[] {r1}) ;
131         //设置管理员组与权限
132         g1.setActions(new Action[] {at01,at02,at03}) ;
133         g2.setActions(new Action[] {at04,at05,at06}) ;
134         g3.setActions(new Action[] {at07,at08,at09}) ;
135         g4.setActions(new Action[] {at10,at11}) ;
136         g5.setActions(new Action[] {at12,at13}) ;
137         at01.setGroup(g1) ;
138         at02.setGroup(g1) ;
139         at03.setGroup(g1) ;
140         at04.setGroup(g2) ;
141         at05.setGroup(g2) ;
142         at06.setGroup(g2) ;
143         at07.setGroup(g3) ;
144         at08.setGroup(g3) ;
145         at09.setGroup(g3) ;
146         at10.setGroup(g4) ;
147         at11.setGroup(g4) ;
148         at12.setGroup(g5) ;
149         at13.setGroup(g5) ;
150         //第二步:取出数据内容
151         System.out.println(a1.getInfo()) ;
152         System.out.println("\t|-" + a1.getRole().getInfo()) ;
153         for (int x = 0 ;x < a1.getRole().getGroups().length ;x ++ ){
154             System.out.println("\t|-" + a1.getRole().getGroups()[x].getInfo()) ;
155             for (int y = 0 ;y< a1.getRole().getGroups()[x].getActions().length ;y ++ ){
156                 System.out.println("\t\t\t|-" + a1.getRole().getGroups()[x].getActions()[y].getInfo()) ;
157             }
158         }
159         System.out.println("-----------------------------------------------------------------") ;
160         System.out.println(g2.getInfo()) ;
161         for (int x = 0 ;x < g2.getRoles().length ; x ++ ){
162             System.out.println("\t|-" + g2.getRoles()[x].getInfo()) ;
163             for (int y = 0 ;y < g2.getRoles()[x].getAdmins().length ;y ++ ){
164                 System.out.println("\t\t|-" + g2.getRoles()[x].getAdmins()[y].getInfo()) ;
165             }
166         }
167     }
168 }

这次使用了win7 自带的PowerShell,运行结果如下

posted @ 2016-03-10 15:00  蒋立  阅读(1500)  评论(2编辑  收藏  举报