首页 > 编程 > Java > 正文

Java复习之对象一对多关系与对象多对一关系

2019-11-06 07:01:49
字体:
来源:转载
供稿:网友

这篇博客主要是在集合的基础上学习一下对象的一对多关系与对象的多对一关系。

1.对象的一对多关系

我们拿古代皇帝与后宫佳丽来举一个例子,皇帝只有一个,而佳丽却有三千之多。皇帝与后宫佳丽之间的关系就是一对多的关系,同时,后宫佳丽与皇帝之间的关系是多对一的关系。

Demo:

public class Emperor { PRivate String name; private String title; //一对多关系 private Set<Concubine> concubines; public Set<Concubine> getConcubines() { return concubines; } public void setConcubines(Set<Concubine> concubines) { this.concubines=concubines; } public Emperor(String title, String name) { this.title = title; this.name = name; } public String getName() { return name; } public String getTitle() { return title; } public void setName(String name) { this.name = name; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "Emperor{" + "name='" + name + '/'' + ", title='" + title + '/'' + '}'; }}public class Concubine { private String name; private String jobs; // private Emperor emperor; public Emperor getEmperor() { return emperor; } //多对一关系 public void setEmperor(Emperor emperor) { this.emperor = emperor; } public Concubine(String name, String jobs) { this.name = name; this.jobs = jobs; } public String getName() { return name; } public String getJobs() { return jobs; } public void setName(String name) { this.name = name; } public void setJobs(String jobs) { this.jobs = jobs; } @Override public String toString() { return "Concubine{" + "name='" + name + '/'' + ", jobs='" + jobs + '/'' + '}'; }}public class OneToManyDemo { public static void main(String args[]) { Emperor e =new Emperor("威哥","威武"); Concubine c1=new Concubine("小白","答应"); Concubine c2=new Concubine("小宝","贵人"); Concubine c3=new Concubine("轩","皇贵妃"); /** * 关联关系 */ //获得concubines集合 e.setConcubines(new HashSet<Concubine>()); //向集合中添加元素 //相互添加进行设置,皇帝对象里面添加妃子,妃子设置皇帝 e.getConcubines().add(c1); e.getConcubines().add(c2); e.getConcubines().add(c3); c1.setEmperor(e); c2.setEmperor(e); c2.setEmperor(e); //通过皇帝得到妃子 System.out.println("皇帝:"+e.getName()+"年号:"+e.getTitle()); Set<Concubine> set=e.getConcubines(); for (Concubine concubine:set) { System.out.println(concubine); } }}

其实仔细看一下代码的实现过程也不难,就在皇帝类中关联妃子的对象,并实现妃子对象的获得与修改的方法,同样在妃子类中关联皇帝的对象,并添加对象的get与set方法。在实现类中,使用皇帝的对象调用妃子的set方法为皇帝绑定多个妃子对象,使用妃子对象的调用皇帝的set的方法,妃子绑定皇帝。

2.对象的多对多关系

我们再举一个生活中的例子,老师与学生的例子,老师教多个学生,学生被多个老师教,老师与同学之间便形成了多对多的关系。

ublic class Teacher { private String name; private int age; //表示多对多关系 private ArrayList<Student> students=new ArrayList<Student>(); public ArrayList<Student> getStudents() { return students; } public void setStudents(ArrayList<Student> students) { this.students = students; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public Teacher(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Teacher{" + "name='" + name + '/'' + ", age=" + age + '}'; }}public class Student { private String name; private int age; //表示多对多关系 private ArrayList<Teacher> teachers =new ArrayList<Teacher>(); public ArrayList<Teacher> getTeachers() { return teachers; } public void setTeachers(ArrayList<Teacher> teachers) { this.teachers = teachers; } public Student(String name, int age) { this.name = name; this.age = age; } public int getAge() { return age; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name='" + name + '/'' + ", age=" + age + '}'; }}public class ManyToManyDemo { public static void main(String args[]) { Teacher t1 =new Teacher("白老师",18); Teacher t2=new Teacher("苍老师",28); Student s1 =new Student("小白",15); Student s2=new Student("小黑",19); //通过get,set方法添加元素,实现多对多 t1.getStudents().add(s1); t1.getStudents().add(s2); t2.getStudents().add(s1); t2.getStudents().add(s2); s1.getTeachers().add(t1); s1.getTeachers().add(t2); s2.getTeachers().add(t1); s2.getTeachers().add(t2); }}

实现的原理与对象的多对一的实现原理是差不多的,不过就是在另外一个类中也是声明对象的集合,而不只是单个对象。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表