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,运行结果如下

Java简单类——多对多映射的更多相关文章

  1. 数据表与简单java类——多对多映射

    给定一张Meber和Product表,得到如下信息: 1.获取一个用户访问的所有商品信息 2.获取一个商品被哪些用户浏览过的信息 package Mapping_transformation; cla ...

  2. Java实体类之间的映射(多对多关系)

    多对对的映射,可以用学生和课程进行演示.一个学生可以选择多个课程,一个课程又对应了多个学生 定义学生类 class Stu{ private String name; private String n ...

  3. java 简单类

    package 面向对象编程_封装性; //1.类名称必须存在有意义,例如Book.Emp //2.类之中所有的属性必须用private封装,封装后的属性必须提供有setter和getter方法 // ...

  4. 第四周总结和实验二Java简单类与对象

    实验目的 掌握类的定义,熟悉属性.构造函数.方法的使用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实列的方法和属性: 理解static修饰对类. ...

  5. 第四周课程总结与第二次实验报告(Java简单类与对象)

    1.写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类具有: ...

  6. Java简单类——双向一对多映射

    class Item { // 父栏目 private int iid ; private String name ; private String note ; private Subitem su ...

  7. Java实体类之间的映射(一对多关系)

    通过栗子,一个人可以有多辆汽车 定义人   这个类 人可以有很多辆汽车,类中车属性用数组 class Person{ private String name; private String phone ...

  8. Java简单类——一对多映射(省、市)

    class Province { private int pid ; private String name ; private City cities [] ; public Province (i ...

  9. Java简单类(部门、领导、雇员关系)

    class Dept { private int deptno ; private String dname ; private String loc ; private Emp emps [] ; ...

随机推荐

  1. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  2. python set

    set是一个工厂函数(filter也是工厂函数),是一个可变的集合 frozenset 不可变的集合,与set共性,他也在内部自动去重, >>> num5=frozenset([1, ...

  3. Python复习之下划线的含义

    __xx__ 系统定义名字 __xx 双下划线的表示的是私有类型的变量.只能是允许这个类本身进行访问了.连子类也不可以 _xx 单下划线 不能用'from moduleimport *'导入 即保护类 ...

  4. JavaScript方法——call和apply

    1.相同点: a) 产生的效果或作用完全相同: b) 至少有一个参数: c) 第一个参数必须有且是一个对象(Object),因为就是这个家伙偷懒. 2.不同点: 传递参数的方式. 前提: 1.有两个对 ...

  5. 使用SQL语句对数据进行MD5加密

    如果数据库表User中有一列为passwd,存放的是md5加密的数据,如何更新新的数据. update user set passwd=md5("123321") where uN ...

  6. ACM/ICPC 之 最小割转网络流(POJ3469)

    重点:构图 //最小割转网络流 //邻接表+Dinic //Time:5797Ms Memory:6192K #include<iostream> #include<cstring& ...

  7. java开发JDBC连接数据库详解

    JDBC连接数据库 好文一定要让大家看见 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机) ...

  8. MyBatis持久层框架使用总结

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis . 2 ...

  9. HDU 3642 Get The Treasury (线段树扫描线)

    题意:给你一些长方体,问你覆盖三次及以上的体积有多大 首先我们观察x轴y轴一样很大,但是z轴很小,所以我们可以枚举z轴(-500,500),注意我们枚举的是每一段长度为一的z轴的xy轴的面积而不是点. ...

  10. Linux学习笔记(2)-开机

    今天开始学习Linux系统. 打开虚拟机,输入密码后,令人激动的画面就蹦出来了-- Ubuntu的主要基调是橙色,给人一种蠢萌蠢萌的感觉,和Windows不同,它只在左边有一条任务栏,上面有些东西,搜 ...