Java系列(七)__模型案例练习

Java系列(七)__模型案例练习

1、第二个代码模型

第二个代码模型:数据表与简单java类映射(核心)

         题目要求,要求使用emp表(empno、ename、job、sal、commmgr、deptno)和dept(deptno、dname、loc)表进行操作,要求可以实现如下的功能:

                   · 功能一:可以输出部门的完整信息,同时输出部门之中所有雇员以及雇员直接领导的信息;

                   · 功能二:可以根据一个雇员找到雇员的领导信息和他所在的部门信息。

         在本程序之中有两个关联的对应关系:

                   · 关系一:雇员和部门之间依靠的deptno字段的联系;

                   · 关系二:雇员和领导之间的联系,依靠mgr字段。

步骤一:编写简单Java类,暂时只包含表之中的基本字段。

class Emp {

         private int empno ;

         private String ename ;

         private String job ;

         private double sal ;

         private double comm ;

         public Emp() {}

         public Emp(int empno,String ename,String job,double sal,double comm) {

                   this.empno = empno ;

                   this.ename = ename ;

                   this.job = job ;

                   this.sal = sal ;

                   this.comm = comm ;

         }

         public String getInfo() {

                   return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:" + this.comm ;

         }

}

class Dept {

         private int deptno ;

         private String dname ;

         private String loc ;

         public Dept() {}

         public Dept(int deptno,String dname,String loc) {

                   this.deptno = deptno ;

                   this.dname = dname ;

                   this.loc = loc ;

         }

         public String getInfo() {

                   return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc ;

         }

}

步骤二:设置关系字段

         · 一对多关系:部门和雇员;

         · 一对一关系:雇员和领导。

class Emp {

         private int empno ;

         private String ename ;

         private String job ;

         private double sal ;

         private double comm ;

         private Dept dept ;        // 一个雇员属于一个部门

         private Emp mgr ;

         public Emp() {}

         public Emp(int empno,String ename,String job,double sal,double comm) {

                   this.empno = empno ;

                   this.ename = ename ;

                  this.job = job ;

                   this.sal = sal ;

                   this.comm = comm ;

         }

         public void setMgr(Emp mgr) {

                   this.mgr = mgr ;

         }

         public Emp getMgr() {

                   return this.mgr ;

         }

         public void setDept(Dept dept) {

                   this.dept = dept ;

         }

         public Dept getDept() {

                   return this.dept ;

         }

         public String getInfo() {

                   return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:" + this.comm ;

         }

}

class Dept {

         private int deptno ;

         private String dname ;

         private String loc ;

         private Emp emps [] ;   // 多个雇员

         public Dept() {}

         public Dept(int deptno,String dname,String loc) {

                   this.deptno = deptno ;

                   this.dname = dname ;

                   this.loc = loc ;

         }

         public void setEmps(Emp emps[]) {

                   this.emps = emps ;

         }

         public Emp [] getEmps() {

                   return this.emps ;

         }

         public String getInfo() {

                   return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc ;

         }

}

步骤三:按照表结构设置数据,同时设置完数据后按照表结构取出

public class TestDemo {

         public static void main(String args[]) {

                   // 第一层次:设置关系

                   // 1、分别设置个个对象实体,彼此之间没有联系

                   Emp ea = new Emp(7369,"SMITH","CLERK",800.0,0.0) ;

                   Emp eb = new Emp(7902,"FORD","MANAGER",2450.0,0.0) ;

                   Emp ec = new Emp(7839,"KING","PRESIDENT",5000.0,0.0) ;

                   Dept dept = new Dept(10,"ACCOUNTING","New York") ;

                   // 2、设置雇员和领导关系

                   ea.setMgr(eb) ;

                   eb.setMgr(ec) ;    // ec没有领导

                   // 3、设置雇员和部门关系

                   ea.setDept(dept) ;          // 雇员和部门

                   eb.setDept(dept) ;         // 雇员和部门

                   ec.setDept(dept) ;          // 雇员和部门

                   Emp [] temp = new Emp[] {ea,eb,ec} ;

                   dept.setEmps(temp) ;

                   // 第二层次:根据关系取数据

                   // 1、取出部门信息

                   System.out.println(dept.getInfo()) ;

                   // 2、取出部门之中的所有雇员信息

                   Emp [] allEmps = dept.getEmps() ; // 先接收

                   for (int x = 0 ; x < allEmps.length ; x ++) {

                            Emp e = allEmps[x] ;    // 取出一个雇员

                            System.out.println("\t" + e.getInfo()) ;

                            if (e.getMgr() != null) {         // 有领导

                                     System.out.println("\t\t|- " + e.getMgr().getInfo()) ;

                            }

                   }

                   // 3、根据雇员取信息

                   System.out.println(ea.getInfo()) ;

                   if (ea.getMgr() != null) {

                            System.out.println(ea.getMgr().getInfo()) ;

                   }

                   if (ea.getDept() != null) {

                            System.out.println(ea.getDept().getInfo()) ;

                   }

         }

}

         在今天学习完之后,必须具备这种简单java类与数据表转换的能力。




2、引用实际案例

1、   一对多映射


        

         可以根据类别输出全部的子类别信息;

class Type {

         private int tid ;     // 类别编号

         private String name ;     // 类别名称

         private String note ;      // 描述

         private SubType subTypes [] ;         // 子类别

         public Type() {}

         public Type(int tid,String name,String note) {

                   this.tid = tid ;

                   this.name = name ;

                   this.note = note ;

         }

         public void setSubTypes(SubType subTypes []) {

                   this.subTypes = subTypes ;

         }

         public SubType[] getSubTypes() {

                   return this.subTypes ;

         }

         public String getInfo() {

                   return "类别编号:" + this.tid + ",名称:" + this.name + ",描述:" + this.note ;

         }

}

class SubType {

         private int stid ;    // 子类别编号

         private String name ;     // 子类别名称

         private String note ;      // 描述

         private Type type ;

         public SubType() {}

         public SubType(int stid,String name,String note) {

                   this.stid = stid ;

                   this.name = name ;

                   this.note = note ;

         }

         public void setType(Type type) {

                   this.type = type ;

         }

         public Type getType(){

                   return this.type ;

         }

         public String getInfo() {

                  return "子类别编号:" + this.stid + ",名称:" + this.name + ",描述:" + this.note ;

         }

}

 

public class TestDemo {

         public static void main(String args[]) {

                   // 第一层次:设置关系

                   Type type = new Type(1,"图形图象处理","处理照片、影片") ;

                   SubType st1 = new SubType(10,"图片编辑","-") ;

                   SubType st2 = new SubType(20,"动画设计","-") ;

                   SubType st3 = new SubType(30,"视频后期","-") ;

                   st1.setType(type) ;

                   st2.setType(type) ;

                   st3.setType(type) ;

                   type.setSubTypes(new SubType[]{st1,st2,st3}) ;

                   // 第二层次:根据关系取数据

                   System.out.println(type.getInfo()) ;

                   for (int x = 0 ; x < type.getSubTypes().length ; x ++) {

                            System.out.println(type.getSubTypes()[x].getInfo()) ;

                   }

         }

}

2、   一对多映射

        

         根据用户取得用户的全部登录日志(不考虑DATE型数据)

class User {

         private String uid ;

         private String password ;

         private int flag ;

         private int level ;

         private int grade ;

         private Log logs [] ;

         public User() {}

         public User(String uid,String password,int flag,int level,int grade) {

                   this.uid = uid ;

                   this.password = password ;

                   this.flag = flag ;

                   this.level = level ;

                   this.grade = grade ;

         }

         public void setLogs(Log logs[]) {

                   this.logs = logs ;

         }

         public Log[] getLogs() {

                   return this.logs ;

         }

         public String getInfo() {

                   return "用户编号:" + this.uid + ",密码:" + this.password + ",审核标志:" + (this.flag == 0 ? "已审核" : "未审核") + "、级别:" + this.level + ",积分" + this.grade ;

         }

}

class Log {

         private int lid ;

         private String ip ;

         private User user ;

         public Log() {}

         public Log(int lid,String ip) {

                   this.lid = lid ;

                   this.ip = ip ;

         }

         public void setUser(User user) {

                   this.user = user ;

         }

         public User getUser() {

                   return this.user ;

         }

         public String getInfo() {

                   return "登录日志编号:" + this.lid + ",IP地址;" + this.ip ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   // 第一层次:设置关系

                   User user = new User("jinbo","wobupasi",0,0,10) ;

                   Log la = new Log(10,"110.110.110.110") ;

                   Log lb = new Log(20,"119.119.119.119") ;

                   Log lc = new Log(30,"112.112.112.112") ;

                   la.setUser(user) ;

                   lb.setUser(user) ;

                   lc.setUser(user) ;

                   user.setLogs(new Log[] {la,lb,lc}) ;

                   // 第二层次:根据关系取数据

                   System.out.println(user.getInfo()) ;

                  for (int x = 0 ; x < user.getLogs().length ; x ++) {

                            System.out.println(user.getLogs()[x].getInfo()) ;

                   }

         }

}

3、   多对多映射


         配置完关系之后,要求可以实现如下的功能:

                   · 可以根据一个管理员取得此管理员所在的所有管理员组,以及每个管理员组所具备的权限;

                   · 可以根据一个管理员组取得此管理员组下的所有管理员和权限信息;

                   · 可以根据一个权限取得具备此权限的所有管理员组,以及每个管理员组的管理员信息。

         如果要想做一个划分的话,本程序严格来讲只是一个简单的多对多关系映射(因为在关系表之中只保存有关联字段,没有额外字段)。

class Admin {

         private String aid ;

         private String password ;

         private int flag ;

         private Group groups []         ;        // 可能存在多个组

         public Admin() {}

         public Admin(String aid,String password,int flag) {

                   this.aid = aid ;

                   this.password = password ;

                   this.flag = flag ;

         }

         public void setGroups(Group groups []) {

                   this.groups = groups ;

         }

         public Group [] getGroups() {

                   return this.groups ;

         }

         public String getInfo() {

                   return "管理员编号:" + this.aid + ",密码:" + this.password + ",级别:" + (this.flag == 0 ? "超级管理员" : "普通管理员") ;

         }

}

class Group {

         private int gid ;

         private String name ;

         private String note ;

         private Admin admins [] ;

         private Privilege privileges [] ;

         public Group() {}

         public Group(int gid,String name,String note) {

                   this.gid = gid ;

                   this.name = name ;

                   this.note = note ;

         }

         public void setAdmins(Admin admins[]){

                   this.admins = admins ;

         }

         public Admin [] getAdmins() {

                   return this.admins ;

         }

         public Privilege [] getPrivileges() {

                   return this.privileges ;

         }

         public void setPrivileges(Privilege privileges[]) {

                   this.privileges = privileges ;

         }

         public String getInfo() {

                   return "管理员组编号:" + this.gid + ",名称:" + this.name + ",描述:" + this.note ;

         }

}

class Privilege {

         private int pid ;

         private String name ;

         private String url ;

         private Group groups [] ;

         public Privilege() {}

         public Privilege(int pid,String name,String url) {

                   this.pid = pid ;

                   this.name = name ;

                   this.url = url ;

         }

         public void setGroups(Group groups[]) {

                   this.groups = groups ;

         }

         public Group [] getGroups() {

                   return this.groups ;

         }

         public String getInfo() {

                   return "权限编号:" + this.pid + ",名称:" + this.name + ",地址:" + this.url ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   // 第一层次:设置关系

                   // 1、设置管理员数据

                   Admin a1 = new Admin("超级管理员","hello",0) ;

                   Admin a2 = new Admin("信息管理员","hello",1) ;

                   Admin a3 = new Admin("数据管理员","hello",1) ;

                   // 2、设置管理员组

                   Group g1 = new Group(1,"超级管理员组","-") ;

                   Group g2 = new Group(2,"普通管理员组","-") ;

                   // 3、设置权限数据

                   Privilege priA = new Privilege(2001,"管理员维护","-") ;

                   Privilege priB = new Privilege(2002,"业务流维护","-") ;

                   Privilege priC = new Privilege(2003,"数据维护","-") ;

                   Privilege priD = new Privilege(2004,"审核维护","-") ;

                   Privilege priE = new Privilege(2005,"员工维护","-") ;

                   // 4、设置管理员和管理员组的关系

                   a1.setGroups(new Group[] {g1,g2}) ;

                   a2.setGroups(new Group[] {g2}) ;

                   a3.setGroups(new Group[] {g2}) ;

                   g1.setAdmins(new Admin[] {a1}) ;

                   g2.setAdmins(new Admin[] {a1,a2,a3}) ;

                   // 4、设置管理员组和权限关系

                   g1.setPrivileges(new Privilege[] {priA,priB,priC,priD,priE}) ;

                   g2.setPrivileges(new Privilege[] {priB,priC,priD}) ;

                   priA.setGroups(new Group[]{g1}) ;

                   priB.setGroups(new Group[]{g1,g2}) ;

                   priC.setGroups(new Group[]{g1,g2}) ;

                   priD.setGroups(new Group[]{g1,g2}) ;

                   priE.setGroups(new Group[]{g1}) ;

                   // 第二层次:根据关系取数据

                   // 1、根据一个管理员取得此管理员所在的所有管理员组,以及每个管理员组所具备的权限

                   System.out.println(a1.getInfo()) ;

                   for (int x = 0 ; x < a1.getGroups().length ; x ++) {

                            System.out.println("\t|- " + a1.getGroups()[x].getInfo()) ;

                            for(int y = 0 ; y < a1.getGroups()[x].getPrivileges().length ; y ++) {

                                     System.out.println("\t\t|- " + a1.getGroups()[x].getPrivileges()[y].getInfo()) ;

                            }

                   }

                   System.out.println("===================================") ;

                   // 2、根据一个管理员组取得此管理员组下的所有管理员和权限信息

                   System.out.println(g1.getInfo()) ;

                   for (int x = 0 ; x < g1.getAdmins().length ; x ++) {

                            System.out.println("\t|- " + g1.getAdmins()[x].getInfo()) ;

                   }

                   for (int x = 0 ; x < g1.getPrivileges().length ; x ++) {

                            System.out.println("\t|- " + g1.getPrivileges()[x].getInfo()) ;

                   }

                   System.out.println("===================================") ;

                   // 3、根据一个权限取得具备此权限的所有管理员组,以及每个管理员组的管理员信息

                   System.out.println(priC.getInfo()) ;

                   for (int x = 0 ; x < priC.getGroups().length ; x ++) {

                            System.out.println("\t|- " + priC.getGroups()[x].getInfo()) ;

                            for (int y = 0 ; y < priC.getGroups()[x].getAdmins().length ; y ++) {

                                     System.out.println("\t\t|- " + priC.getGroups()[x].getAdmins()[y].getInfo()) ;

                            }

                   }

         }

}

         以上的这类题目也是为日后的所有程序做准备,而这些题目可以完整的帮助我们巩固所有学习到的内容。




3、第三个代码模型

第三个代码模型:对象比较(重点)

         在讲解具体的概念之前,再来观察一种引用传递的形式,本类接收本类对象。

范例:观察程序代码(暂时不要去思考代码意义)

class Person {

         private String name ;

         public Person(String name) {

                   this.name = name ;

         }       // 接收本类对象

         public void change(Person temp) {

                   temp.name = "李四" ;        // 直接通过“对象.属性”调用

         }

         public String getName() {

                   return this.name ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   Person per = new Person("张三") ;

                   System.out.println(per.getName()) ;

                   per.change(per) ;        // 接收本类,不关心意义,只关心语法

                   System.out.println(per.getName()) ;

         }

}

         正常情况下如果使用了private封装,那么外部是无法通过“对象.属性”的形式进行操作的,但是如果说现在把对象传递回了类之中。

         那么如果说现在有两个Person类对象(name、age属性),那么如果要比较两个对象是否相等,该怎么做?将每一个属性分别进行比较,如果全都相同,则表示相等,于是现在就得出了以下的一段代码。

class Person {

         private String name ;

         private int age ;

         public Person() {}

         public Person(String name,int age) {

                   this.name = name ;

                   this.age = age ;

         }

         public String getName() {

                   return this.name ;

         }

         public int getAge() {

                   return this.age ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   Person perA = new Person("张三",20) ;

                   Person perB = new Person("张三",20) ;

                   if (perA.getName().equals(perB.getName()) &&

                            perA.getAge() == perB.getAge()) {

                            System.out.println("两个对象相等。") ;

                   } else {

                            System.out.println("两个对象不等。") ;

                   }

         }

}

         此时已经可以成功的进行了对象比较操作,但是这种代码有问题。

         此时的代码所有的验证功能都交给主方法去完成,实际上主方法就是一个客户端,在主方法之中不应该牵扯到过多的业务逻辑,只需要进行简单的操作即可。另外,信息的比较过程,应该是每个对象都具备的功能。应该是在类之中定义好的。

class Person {

         private String name ;

         private int age ;

         public Person() {}

         public Person(String name,int age) {

                   this.name = name ;

                   this.age = age ;

         }

         public String getName() {

                   return this.name ;

         }

         public int getAge() {

                   return this.age ;

         }

         // 暂定此方法名称为compare()

         // 此方法会具备两个对象:当前对象this、传入的person

         public boolean compare(Person person) {

                   if (this == person) {    // 地址相同

                            return true ;

                   }

                   if (person == null) {

                            return false ;

                   }

                   // 当对象传回到类之中的时候可以直接利用“对象.属性”访问

                   if (this.name.equals(person.name) && this.age == person.age) {

                            return true ;

                   }

                   return false ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   Person perA = new Person("张三",20) ;

                   Person perB = new Person("张三",20) ;

                   if (perA.compare(null)) {

                            System.out.println("两个对象相等。") ;

                   } else {

                            System.out.println("两个对象不等。") ;

                   }

         }

}

         从标准来讲,简单Java类之中是需要提供有对象比较操作的,回去自己写几个。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值