[阿里云大学][Java面向对象开发][课程笔记][17-42课时]

课时17 数组的定义与使用(数组转置)
首尾交换
1. 新定义一个空数组,然后把原数组的值从后到前存入新的数组
问题:开辟了两块相同的堆内存空间,造成浪费
2. 在原数组上反转
计算数组长度/2,交换次数为n,n = (array.length -1)/2
0 和 length-1交换
1 和 length-2交换
2 和 length-3交换
n 和 length-n-1交换

课时18 数组的定义与使用(二分查找法)
指定的数组中查找某个元素的位置
前提:数组先排序
mid = head/2 + tail/2
原理
比如一个排序好的数组
0,1,2,3,4,5,6,7,8

查找7
第一次查找
from:0
to:8
mid:4
索引为4的数比7小,所以赋值(第二次查找)
from:4+1
to:8
mid:(5+8)/2 = 6
索引为6的数比7小,所以赋值(第三次查找)
from:6+1
to:8
mid:(7+8)/2 = 7
索引为7的数为7,返回索引7

查找2
第一次查找
from:0
to:8
mid:4
索引为4的数比2大,所以赋值(第二次查找)
from:0
to:4-1
mid:(0+3)/2 = 1
索引为1的数比2小,所以赋值(第三次查找)
from:1+1
to:3
mid:(2+3)/2 = 2
索引为2的数为2,返回索引2

递归的结束条件是当from >= to

课时19 数组的定义与使用(对象数组)
核心掌握
引用数据类型为主,类或者接口
对象数组动态初始化: 类名称[] 对象数组名 = new 类名称[长度]
对象数组的静态初始化:类名称[] 对象数组名 = new 类名称[] {对象名,...}
class Person {
    private String name;
    private int age;
    public Person(String setName,int setAge) {
        name = setName;
        age = setAge;
    }
    public String getInfo() {
        return "name is " + name + " age is " + age;
    }
}

public class ArrayDemo {
    public static void main(String[] args) {
        Person[] per = new Person[3]; //动态初始化,默认值为null
        per[0] = new Person("A",1);
        per[1] = new Person("B",2);
        per[2] = new Person("C",3);
        for (int x = 0; x < per.length; x++) {
            System.out.println(per[x].getInfo());
        }
        Person[] per2 = new Person[] { new Person("D",4) }; //静态初始化
        System.out.println(per2[0].getInfo());
    }
}

对象保存的是堆内存的地址


普通数据数组堆内存直接保存数据,比如new int[]
而对象数组堆内存表示各个对象的真是数据地址(里面没有数据),如上图

课时20 String类的基本特点(String类两种实例化方式)
所有开发过程中都存在String类

第一种
String str = "hello";
str是一个对象,hello是保存在堆内存中

第二种
String str = new String("hello");
传入"hello"的名为String的构造函数

课时21 String类的基本特点(字符串比较)

== 和 equals的区别

==比较的是堆内存的地址,是数值比较
equals()比较的是字符串内容,区分大小小

String str1 = "hello"; //把一个匿名对象值为"hello"的对象命名为str1
String str2 = new String("hello");

str1 == str2 结果为false
str1.equals(str2) 结果为true

开发过程中必须使用equals()

课时22 String类的基本特点(字符串常量为匿名对象)


String str1 = "hello"; //把一个匿名对象值为"hello"的对象命名为str1

开发过程中,如果要判断用户输入的字符串是否等同于指定的字符串,一定要把字符串写在前面,考虑用户没有输入数据的问题

操作方法1:字符串写在前面,不会报空指向异常


课时23 String类的基本特点(String两种实例化区别)

第一种 直接赋值(开发推荐做法,节省内存)
String str = "hello";
str是一个对象,hello是保存在堆内存中

多次赋值(指向的是同一个堆内存),使用了共享设计模式
JVM维护对象池, 直接赋值的时候,第一次会保存到对象池,后续如果有一样的对象,就引用一样的对象
也就是一个对象数组,可以减少同样字符串的内存开销




第二种,构造方法(标准做法)
(从右往左执行)

String str = new String("hello");
传入"hello"的名为String的构造函数

会开辟2个堆内存空间,其中一个成为垃圾空间
而且这个对象没有自动保存到对象池

实现入池的操作
public String intern();




课时24 String类的基本特点(字符串常量不可变更)

字符串对象变更:




字符串内容本身是不会变化的,变化的是字符串对象的引用

开发原则:
字符串采用直接赋值模式完成
比较实用equals()方法实现
字符串不要频繁改变

课时25 String类的常用方法(DOC文档组成)



文档组成:
1. 类的相关定义,类的名字,父类,接口等
2. 类的简介
3. 成员(field)摘要,属性就是一种成员,
4. 构造(constructor)方法说明,Deprecated建议不用
5. 方法(methods)摘要,返回值,参数说明


课时26 String类的常用方法(字符串与字符数组)

字符数组 变成 字符串

public String(char[] value) //构造方法
public String(char[] value, int offset, int count) // offset开始,count个数,构造方法
public char charAt(int index) //返回字符串索引位置的字符,索引从0开始 开发中出现的几率很低,普通方法


字符串 变成 字符数组
public char[] toCharArray() //普通方法

重点:字符串和字符数组的互相转换
public class StringDemo {
    public static void main(String[] args) {
        String str = "hello world";
        System.out.println("index 2 " + str.charAt(2));
        char[] data = str.toCharArray();
        for (char x : data) {
            x -= 32; //转换成大写
            System.out.print(x + "/");
        }
        System.out.println(new String(data));
        System.out.println(new String(data,6,5));
    }
}

课时27 String类的常用方法(字节与字符串)
字节:数据传输或者编码转换

支持:
构造方法:
public String(byte[] byte)
public String(byte[] byte, int offset, int length)

普通方法:
public byte[] getBytes()
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException //编码转换

public class StringDemo {
    public static void main(String[] args) {
        String str = "hello world";
        byte data[] = str.getBytes();
        for (byte x : data) {
            System.out.print(x + ",");
        }
    }
}

字节 -128 到 127 无法表示中文
字符才用于表示中文

课时28 String类的常用方法(字符串比较)
equals()方法
区分大小写
public boolean equals(String anotherString)
不区分
public boolean equalsIgnoreCase(String anotherString)


public int compareTo(String anotherString) //比较两个字符串的大小关系
相等返回0
如果不相等:
小于(每个字符进行对比,根据编码数字差返回第一个不相等的字符差)
大于(每个字符进行对比,根据编码数字差返回第一个不相等的字符差)
这个方法很重要

public class StringDemo {
    public static void main(String[] args) {
        String str1 = "hello";
        System.out.println("Hello".equals(str1));
        System.out.println("Hello".equalsIgnoreCase(str1));
        
        System.out.println("A".compareTo("a"));
        System.out.println("a".compareTo("A"));
        
        System.out.println("abb".compareTo("adc"));
    }
}
/*
E:\01_JAVA\project\java_basic\mldn\02\course_28>java StringDemo
false
true
-32
32
-2
*/


课时29 String类的常用方法(字符串查找)- 重要

contains() // 普通方法,判断一个子字符串是否存在,返回boolean JDK1.5之后追加

indexOf() // 普通方法,查找子字符串的索引,返回第一个字符的位置索引,不存在则返回-1

indexOf(String str, int index) //从指定位置开始查找

lastIndexOf() //从后向前找

lastIndexOf(String str, int index) //从指定位置从后往前找

startsWith(String prefix) // 由指定子字符串开头
startsWith(String prefix, int toffset) //从指定位置开始判断,由指定子字符串开头

endsWith(String prefix) //由指定子字符串结尾

建议使用contains()


课时30 String类的常用方法(字符串替换)

指定一个新的字符串替换字符串中的某个字符串

//需要替换的字符串 目标字符串

replaceAll(String regex, String replacement)

replaceFirst(String regex, String replacement)


课时31 String类的常用方法(字符串拆分)

string[] split(String regx)

string[] split(String regx, int limit) // 部分拆分,分成几段,2 代表2段 最大长度,如果没有到最大长度,那就有多少拆多少

\\转义字符


课时32 String类的常用方法(字符串截取)

substring(int beginindex) //从指定索引截取到结尾
substring)int beginindex, int endindex) //指定区间截取
索引从0开始



课时33 String类的常用方法(字符串其它操作方法)

trim() //去掉字符串的左右空格,一个或者多个

toUpperCase() //转大写
toLowerCase() //转小写
这两个函数,如果不是字母,那么就不做转换

intern() //字符串存入对象池

contact(String str) //字符串连接,跟+一样



length() //字符串长度

isEmpty() //判断是否为空字符串,长度为0,非null

没有提供首字符大写的方法

实现首字母大写的方法



课时34 this关键字(this调用属性)

this 可以调用
1. 本类属性
2. 本类方法(构造和普通)
3. 当前对象(相对概念)

下面赋值运行之后,name 为 null,age 为 0;
程序以大括号为边界,不会去找外部定义的属性name



参数与属性同名,解决方法


类的方法中有需要用到属性,一定在前面加this关键字
课时35 this关键字(this调用方法)

this.函数名称(参数)
this.构造方法名称(参数)

区分方法的定义来源(继承中有用)

java支持类构造方法的互相调用:
例子,使用2个参数的调用,先调用1个参数的,再赋值age,调用1个参数的时候,使用无参数的构造函数先输出一段信息,然后再赋值name


有以下几点要求
this()

1. this()必须放在构造方法的首行
2. 使用this()构造方法的时候留出口(递归死循环)(比如在上面这个例子中无参构造里面调用2个参数的构造函数)


课时36 this关键字(表示当前对象)



this指向p1


课时37 引用传递进阶分析

java核心

3个简单例子

第一个:
class Message {
    private int num ;
    public void setNum(int num) {
        this.num = num ;
    }
    public int getNum() {
        return this.num ;
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Message msg = new Message();
        msg.setNum(100);
        fun(msg);
        System.out.println(msg.getNum());
    }
    public static void fun(Message temp) {
        temp.setNum(30);
    }
}



第二个:
public class TestDemo2 {
    public static void main(String[] args) {
        String str = "hello";
        fun(str);
        System.out.println(str);
    }
    public static void fun(String temp) {
        temp = "world";
    }
}



第三个:
class Message2 {
    private String note ;
    public void setNote(String note) {
        this.note = note ;
    }
    public String getNote() {
        return this.note ;
    }
}

public class TestDemo3 {
    public static void main(String[] args) {
        Message2 msg = new Message2();
        msg.setNote("hello");
        fun(msg);
        System.out.println(msg.getNote());
    }
    public static void fun(Message2 temp) {
        temp.setNote("world");
    }
}




课时38 【第02个代码模型】综合案例:对象比较

比较两个对象的属性
实现形式1
class Person {
    private String name ;
    private int age ;
    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 TestDemo1 {
    public static void main(String[] args) {
        Person perA = new Person("A",20);
        Person perB = new Person("A",20);
        System.out.println(perA == perB);
        
        if(perA.getAge() == perB.getAge() && perA.getName().equals(perB.getName())) {
            System.out.println(" perA == perB ");
        }
    }
}

// 这种形式在开发过程中不会出现,主方法需要出现的逻辑太多了

方法2
类方法自带比较方法
compare()
class Person {
    private String name ;
    private int age ;
    public Person(String name, int age) {
        this.name = name ;
        this.age = age ;
    }
    public String getName() {
        return this.name ;
    }
    public int getAge() {
        return this.age ;
    }
    //this表示当前对象,另外一个是传入对象
    public boolean compare(Person per) {
        if (per == this) {
            return true;
        }
        if (per == null) {
            return false;
        }
        if (this.name.equals(per.name) && this.age == per.age) {
            return true;
        } else {
            return false;
        }
    }
}

public class TestDemo2 {
    public static void main(String[] args) {
        Person perA = new Person("A",20);
        Person perB = new Person("A",20);
        System.out.println(perA.compare(perB));
    }
}

判断步骤:
1. 判断地址
2. 判断是否为空
3. 判断对象的各个属性

课时39 引用传递实际应用
引用传递是java核心
合成设计模式

 class Member {
    private String name;
    private int age;
    private Member child;
    //car == null, 说明此人无车
    private Car car;
    public Member(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return this.age;
    }
    public void setChild(Member child) {
        this.child = child;
    }
    public Member getChild() {
        return this.child;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    public Car getCar() {
        return this.car;
    }
    public String getInfo() {
        return "[Member] name = " + this.name + " age = " + this.age;
    }
}

class Car {
    private String car;
    private double price;
    private Member member;
    public Car(String car, double price) {
        this.car = car;
        this.price = price;
    }
    public void setCar(String car) {
        this.car = car;
    }
    public String getCar() {
        return this.car;
    }
    public void setPirce(double price) {
        this.price = price;
    }
    public double getPrice() {
        return this.price;
    }
    public void setMem(Member member) {
        this.member = member;
    }
    public Member getMember() {
        return this.member;
    }
    public String getInfo() {
        return "[car] car = " + this.car + " price = " + this.price;
    }
}

public class TestDemo {
    public static void main(String[] main) {
        Member mem = new Member("A",30);
        Car car = new Car("car-a",100);
        Member child = new Member("B",2);
        Car childCar = new Car("car-b",200);
        
        mem.setCar(car);
        mem.setChild(child);
        car.setMem(mem);
        child.setCar(childCar);
        childCar.setMem(child);
        
        System.out.println(mem.getInfo());
        System.out.println(mem.getCar().getInfo());
        System.out.println(car.getMember().getInfo());
        System.out.println(mem.getChild().getInfo());
        System.out.println(mem.getChild().getCar().getInfo());
    }
}

课时40 【第03个代码模型】综合案例:数据表与简单Java类(一对多)

/**
1. 先定义基本类,包括属性,构造函数,getInfo()函数
2. 定义各个类之间的关系,雇员的领导,部门里面的所有雇员,雇员属于哪个部门
3. 实现开发需求
(1) 设置类对象间的关系
(2) 获取数据
*/

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 this.dept ;
    }
    public String getInfo() {
        return "[Emp] empno = " + empno
        + " ename = " + ename
        + " job = " + job
        + " sal = " + sal
        + " comm = " + 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 "[Dept] deptno = " + deptno
        + " dname = " + dname
        + " loc = " + loc;
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Dept dept = new Dept(10,"accounting","new york");
        Emp ea = new Emp(1,"A","Job-a",100.0,1.0);
        Emp eb = new Emp(1,"B","Job-b",200.0,2.0);
        Emp ec = new Emp(1,"C","Job-c",300.0,3.0);
        
        ea.setMgr(eb);
        eb.setMgr(ec);
        
        ea.setDept(dept);
        eb.setDept(dept);
        ec.setDept(dept);
        
        dept.setEmps(new Emp[] {ea,eb,ec});
        System.out.println(dept.getInfo());
        for (Emp emp : dept.getEmps()) {
            System.out.println("\t |-" + emp.getInfo());
            if (emp.getMgr() != null) {
                System.out.println("\t\t |-" + emp.getMgr());
            }
        }
        System.out.println(eb.getInfo());
        if (eb.getMgr() != null) {
            System.out.println("\t |-" + eb.getMgr().getInfo());
        }
        if (eb.getDept() != null) {
            System.out.println("\t\t |-" + eb.getDept().getInfo());
        }
    }
}

课时41 【第03个代码模型】综合案例:数据表与简单Java类(多对多)

/**
基本信息:
学生表:编号,姓名,年龄,
课程表:编号,名称,学分
学生-课程关系表:学生编号,课程编号,成绩

需求:
1. 找到一门课程,参加此课程的所有学生信息和成绩
2. 找到一个学生,参加的课程的课程信息和成绩

设计过程:
1. 定义基本类,前2个表,

2. 第三个表是学生选课信息

3. 筛选关系,去掉第一步做的学生中的课程对象数组,取出课程中的学生数组,因为第二部中设计的表可以
表达关系了

4. 输出关系


*/
class Student {
    private int studentId;
    private String studentName;
    private int studentAge;
    private StudentCourse[] studentCourse;
    public Student() {}
    public Student(int studentId, String studentName, int studentAge) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.studentAge = studentAge;
    }
    public void setStudentCourse(StudentCourse[] studentCourse) {
        this.studentCourse = studentCourse;
    }
    public StudentCourse[] getStudentCourse() {
        return this.studentCourse;
    }
    public String getInfo() {
        return "[Student] ID = " + this.studentId
        + " Name = " + this.studentName
        + " Age = " + this.studentAge;
    }
}
class Course {
    private int courseId;
    private String courseName;
    private int credit;
    private StudentCourse[] studentCourse;
    public Course() {}
    public Course(int courseId, String courseName, int credit) {
        this.courseId = courseId;
        this.courseName = courseName;
        this.credit = credit;
    }
    public void setStudentCourse(StudentCourse[] studentCourse) {
        this.studentCourse = studentCourse;
    }
    public StudentCourse[] getStudentCourse() {
        return this.studentCourse;
    }
    public String getInfo() {
        return "[Course] ID = " + this.courseId
        + " Name = " + this.courseName
        + " credit = " + this.credit;
    }
}
class StudentCourse {
    private Student student;
    private Course course;
    private double score;
    
    public StudentCourse() {}
    public StudentCourse(Student student, Course course, double score) {
        this.student = student;
        this.course = course;
        this.score = score;
    }
    public Student getStudent() {
        return this.student;
    }
    public Course getCourse() {
        return this.course;
    }
    public double getScore() {
        return this.score;
    }
    public String getInfo() {
        return //this.student.getInfo()
        //+ this.course.getInfo()
        //+
        "[score] score = " + this.score;
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Student a = new Student(1,"a",18);
        Student b = new Student(2,"b",19);
        Student c = new Student(3,"c",20);
        
        Course ca = new Course(1,"ca",10);
        Course cb = new Course(2,"cb",20);
        Course cc = new Course(3,"cc",30);
        
        a.setStudentCourse(new StudentCourse[] {
            new StudentCourse(a,ca,100.0),
            new StudentCourse(a,cb,100.0),
            new StudentCourse(a,cc,100.0)
        });
        b.setStudentCourse(new StudentCourse[] {
            new StudentCourse(b,ca,90.0),
            new StudentCourse(b,cb,90.0),
            new StudentCourse(b,cc,90.0)
        });
        c.setStudentCourse(new StudentCourse[] {
            new StudentCourse(c,ca,80.0),
            new StudentCourse(c,cb,80.0),
            //new StudentCourse(b,cc,90)
        });
        
        ca.setStudentCourse( new StudentCourse[] {
            new StudentCourse(a,ca,100.0),
            new StudentCourse(b,ca,90.0),
            new StudentCourse(c,ca,80.0)
        });
        cb.setStudentCourse( new StudentCourse[] {
            new StudentCourse(a,cb,100.0),
            new StudentCourse(b,cb,90.0),
            new StudentCourse(c,cb,80.0)
        });
        cc.setStudentCourse( new StudentCourse[] {
            new StudentCourse(a,cb,100.0),
            new StudentCourse(b,cb,90.0)
        });
        
        System.out.println("******************************");
        System.out.println(a.getInfo());
        System.out.println(b.getInfo());
        System.out.println(c.getInfo());
        
        System.out.println("******************************");
        System.out.println(ca.getInfo());
        System.out.println(cb.getInfo());
        System.out.println(cc.getInfo());
        
        System.out.println("******************************");
        System.out.println(ca.getInfo());
        for (int x = 0; x < ca.getStudentCourse().length; x++) {
            System.out.println("\t |- " + ca.getStudentCourse()[x].getStudent().getInfo() + "\n\t\t |- " + ca.getStudentCourse()[x].getInfo());
        }
        System.out.println("******************************");
        System.out.println(a.getInfo());
        for (int x = 0; x < a.getStudentCourse().length; x++) {
            System.out.println("\t |- " + a.getStudentCourse()[x].getCourse().getInfo() + "\n\t\t |- " + ca.getStudentCourse()[x].getInfo());
        }
    }
}

课时42 【第03个代码模型】综合案例:数据表与简单Java类(角色与权限)

1. 进行单独类的描述
Dept //部门
Emp //员工
Role //角色
Privilege //权限

属性
构造函数
getInfo()函数

2. 进行关系的描述
一个部门有多个员工 ,且只有一个角色
员工只有一个部门
角色有多个部门,多个权限
权限有多个角色
角色-权限是2个外键(没有其他多的数据,上个例子有个成绩),不需要建表

3. 实现数据输出
创建部门数据,2个
创建员工数据,5个
创建角色信息,2个
创建权限数据,4个

部门和雇员
雇员和部门

部门和角色
角色和部门

设置角色和权限的关系
设置权限和角色的关系

4. 取出数据

完毕

代码

class Dept {
    private int deptID;
    private String deptName;
    private Emp[] emps;
    private Role role;
    
    public Dept() {}
    public Dept(int deptID, String deptName) {
        this.deptID = deptID;
        this.deptName = deptName;
    }
    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }
    public Emp[] getEmps() {
        return this.emps;
    }
    public void setRole(Role role) {
        this.role = role;
    }
    public Role getRole() {
        return this.role;
    }
    public String getInfo() {
        return "[Dept] deptID = " + this.deptID
        + " deptName = " + this.deptName;
    }
}
class Emp {
    private int empId;
    private String empName;
    private Dept dept;
    
    public Emp() {}
    public Emp(int empId, String empName) {
        this.empId = empId;
        this.empName = empName;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public Dept getDept(){
        return this.dept;
    }
    public String getInfo() {
        return "[Emp] empId = " + this.empId
        + " empName = " + this.empName;
    }
}
class Role {
    private int roleId;
    private String roleName;
    private Dept[] depts;
    private Privilege[] privileges;
    
    public Role() {}
    public Role(int roleId, String roleName) {
        this.roleId = roleId;
        this.roleName = roleName;
    }
    public void setDepts(Dept[] depts) {
        this.depts = depts;
    }
    public Dept[] getDepts() {
        return this.depts;
    }
    public void setPrivileges(Privilege[] privileges) {
        this.privileges = privileges;
    }
    public Privilege[] getPrivileges() {
        return this.privileges;
    }
    public String getInfo() {
        return "[Role] roleId = " + this.roleId
        + " roleName = " + roleName;
    }
}
class Privilege {
    private int pId;
    private String pName;
    private String pFlag;
    private Role[] roles;
    
    public Privilege() {}
    public Privilege(int pId, String pName, String pFlag) {
        this.pId = pId;
        this.pName = pName;
        this.pFlag = pFlag;
    }
    public void setRoles(Role[] roles) {
        this.roles = roles;
    }
    public Role[] getRoles() {
        return this.roles;
    }
    public String getInfo() {
        return "[Privilege] pId = " + pId
        + " pName = " + pName
        + " pFlag = " + pFlag;
    }
}

public class TestDemo {
    public static void main(String[] args) {
        Dept deptA = new Dept(1,"财务部");
        Dept deptB = new Dept(2,"技术部");
        Dept deptC = new Dept(3,"行政部");
        
        Emp empA = new Emp(1,"员工A");
        Emp empB = new Emp(2,"员工B");
        Emp empC = new Emp(3,"员工C");
        Emp empD = new Emp(4,"员工D");
        Emp empE = new Emp(5,"员工E");
        
        Role roleA = new Role(1,"普通员工");
        Role roleB = new Role(2,"高级员工");
        
        Privilege PA = new Privilege(1000,"查询","正常");
        Privilege PB = new Privilege(1001,"删除","正常");
        Privilege PC = new Privilege(1002,"修改","正常");
        
        empA.setDept(deptA);
        empB.setDept(deptA);
        empC.setDept(deptB);
        empD.setDept(deptB);
        empE.setDept(deptC);
        
        deptA.setEmps(new Emp[] {empA,empB});
        deptB.setEmps(new Emp[] {empC,empD});
        deptC.setEmps(new Emp[] {empE});
        
        deptA.setRole(roleA);
        deptB.setRole(roleA);
        deptC.setRole(roleB);
        
        roleA.setDepts(new Dept[] {deptA,deptB});
        roleB.setDepts(new Dept[] {deptC});
        
        roleA.setPrivileges(new Privilege[] {PA});
        roleB.setPrivileges(new Privilege[] {PA,PB,PC});
        
        PA.setRoles(new Role[] {roleA,roleB});
        PB.setRoles(new Role[] {roleB});
        PC.setRoles(new Role[] {roleB});
        
        System.out.println(empA.getInfo());
        System.out.println("\t |-" + empA.getDept().getInfo());
        System.out.println("\t\t |-" + empA.getDept().getRole().getInfo());
        for (Privilege x : empA.getDept().getRole().getPrivileges()) {
            System.out.println("\t\t\t |-" + x.getInfo());
        }
        System.out.println(roleA.getInfo());
        for (Dept x : roleA.getDepts()) {
            System.out.println("\t |- " + x.getInfo());
            for (Emp y : x.getEmps()) {
                System.out.println("\t\t |- " + y.getInfo());
            }
        }
        System.out.println(PA.getInfo());
        for (Role x : PA.getRoles()) {
            System.out.println("\t |- " + x.getInfo());
            for (Dept y : x.getDepts()) {
                System.out.println("\t\t |- " + y.getInfo());
                for (Emp z : y.getEmps()) {
                System.out.println("\t\t\t |- " + z.getInfo());
            }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值