• Java中数据表的建立


    class Emp{
        private int empno;//职工编号
        private String ename;//姓名
        private String job;//职位
        private double sal;//基本工资
        private double comm;
        private Emp mgr;//所属领导
        private  Dept dept;//所在部门
    
        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 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 emps;
        }
    
        public String getInfo(){
            return "部门编号:" + this.deptno +",部门名称:" + this.dname +",所在地址:" + this.loc;
        }
    }
    public class Main {
        public static void main(String[] args) {
            // 创建各自的实例化对象
            Dept dept = new Dept(10,"ACCOUNTING","NEW YORK");
            Emp ea = new Emp(7369,"SMITH","CLERK",800.0,0.0);
            Emp eb = new Emp(7466,"ALLEN","MANAGER",2450.0,0.0);
            Emp ec = new Emp(7839,"KING","PRESIDENT",5000.0,0.0);
            //设置领导关系
            ea.setMgr(eb);
            eb.setMgr(ec);//ec对象没有领导,因为他是头
            //设置员工和部门关系
            ea.setDept(dept);
            eb.setDept(dept);
            ec.setDept(dept);
            //设置部门和员工的关系
            dept.setEmps(new Emp[]{ea,eb,ec});
    
            //读取数据
            System.out.println(dept.getInfo());//输出部门信息
            for(int x=0;x<dept.getEmps().length;x++){
                System.out.println("	"+dept.getEmps()[x].getInfo());
                if(dept.getEmps()[x].getMgr()!=null){ //若有领导
                    System.out.println("	"+dept.getEmps()[x].getMgr().getInfo());
                }
            }
            System.out.println("************************");
            //根据员工找到对应的领导信息和部门信息
            System.out.println(eb.getInfo());
            if(eb.getMgr()!=null)
                System.out.println("	"+eb.getMgr().getInfo());
            if(eb.getDept()!=null)
                System.out.println("	"+eb.getDept().getInfo());
        }
    }

    上面代码是实现一对多的数据表,基于公司人事管理的例子(根据员工可以输出其基本信息及所在部门信息和所属领导信息,根据部门可以输出所有员工及领导),在Java一对多的数据关系中,需要遵循以下设计原则:

    简单Java类设计原则:Java类与数据表的关系

      1、Java的名称 = 实体表的名称

      2、Java类的属性 = 实体表的字段

      3、Java类的一个对象 = 表的一行记录

      4、对象数组 = 表的多行记录

      5、外键关系 = 引用配置

    多对多的数据表(学生成绩管理):根据学生可以输出所修课程信息及成绩,根据课程可以输出学习该课程的学生信息及成绩

    class Student{//学生表
        private int stuid;
        private String sname;
        private int age;
        private StudentCourse studentCourse[];//学生成绩信息
        public Student(){}
        public Student(int stuid,String sname,int age){
            this.stuid = stuid;
            this.sname = sname;
            this.age = age;
        }
        public void setStudentCourse(StudentCourse[] studentCourse) {
            this.studentCourse = studentCourse;
        }
    
        public StudentCourse[] getStudentCourse() {
            return studentCourse;
        }
    
        public String getInfo(){
            return "学号:" + this.stuid + ",学生姓名:" + this.sname + ",年龄:" + this.age;
        }
    }
    class Course{//课程表
        private String name;
        private int cid;
        private int credit;
        private StudentCourse studentCourse[];
        public Course(){}
        public Course(String name,int cid,int credit){
            this.cid = cid;
            this.name = name;
            this.credit = credit;
        }
    
        public StudentCourse[] getStudentCourse() {
            return studentCourse;
        }
    
        public void setStudentCourse(StudentCourse[] studentCourse) {
            this.studentCourse = studentCourse;
        }
    
        public String getInfo(){
            return "课号:" + this.credit + ",名称:" + this.name + ",学分:" + this.credit;
        }
    }
    class StudentCourse{//学生选课表
        private Student student;
        private Course course;
        private double score;//成绩
        public StudentCourse(){
        }
        public StudentCourse(Student student,Course course,double score){
            this.course = course;
            this.score = score;
            this.student = student;
        }
    
        public Course getCourse() {
            return course;
        }
    
        public Student getStudent() {
            return student;
        }
    
        public double getScore() {
            return score;
        }
    }
    public class Main {
    
        public static void main(String[] args) {
            //创建各自的独立对象
            Student stu1 = new Student(1,"张三",18);
            Student stu2 = new Student(1,"李四",20);
            Student stu3 = new Student(1,"王五",19);
            Course ca = new Course("高等数学",1001,5);
            Course cb = new Course("线性代数",1002,4);
            //设置各自的关系
            //设置学生和课程的关系
            stu1.setStudentCourse(new StudentCourse[]{
                    new StudentCourse(stu1,ca,92.5),
                    new StudentCourse(stu1,cb,94.0)
            });
            stu2.setStudentCourse(new StudentCourse[]{
                    new StudentCourse(stu2,ca,89.0)
            });
            stu3.setStudentCourse(new StudentCourse[]{
                    new StudentCourse(stu3,cb,95.0),
                    new StudentCourse(stu3,ca,90.5)
            });
            //设置课程和学生的关系
            ca.setStudentCourse(new StudentCourse[]{
                    new StudentCourse(stu1,ca,92.5),
                    new StudentCourse(stu2,ca,89.0)
                    new StudentCourse(stu3,ca,90.5)
            });
            cb.setStudentCourse(new StudentCourse[]{
                    new StudentCourse(stu1,cb,94.0),
                    new StudentCourse(stu3,cb,95.0)
            });
            //找到一门课程,并且输出学习此课程的所有学生的信息及成绩
            System.out.println(ca.getInfo());
            for(int x = 0;x < ca.getStudentCourse().length;x++){
                System.out.println("	"+ca.getStudentCourse()[x].getStudent().getInfo()
                        +",成绩:"+ca.getStudentCourse()[x].getScore());
            };
            System.out.println("***********");
            //根据学生输出其信息及选修的课程信息和所得成绩
            System.out.println(stu1.getInfo());
            for(int x = 0;x < stu1.getStudentCourse().length;x++){
                System.out.println("	选修课程:" + stu1.getStudentCourse()[x].getCourse().getInfo()
                        + ",所得成绩:" + stu1.getStudentCourse()[x].getScore());
            }
    
        }
    }
  • 相关阅读:
    第三周进度条
    团队作业个人博客05
    团队作业个人博客04
    用户分析,场景分析
    团队作业个人博客03
    团队作业个人博客02
    团队作业个人博客01
    第四周进度条
    第三周进度条
    四则运算2
  • 原文地址:https://www.cnblogs.com/attentle/p/8758617.html
Copyright © 2020-2023  润新知