正则表达式匹配网站,网站建设设计指标,织梦可以做婚纱影楼网站吗,班级网站的规划与建设使用集合不仅可以表示一对一的关系#xff0c;也可以表示多对多的关系。例如#xff0c;一个学生可以选多门课程#xff0c;一门课程可以有多个学生参加#xff0c;那么这就是一个典型的多对多关系。
要完成上面要求#xff0c;首先应该定义两个类#xff0c;分别是学生…使用集合不仅可以表示一对一的关系也可以表示多对多的关系。例如一个学生可以选多门课程一门课程可以有多个学生参加那么这就是一个典型的多对多关系。
要完成上面要求首先应该定义两个类分别是学生信息Student类、课程信息Course类。在学生类中存在一个集合保存全部的课程。同样在课程类中也要存在一个集合保存全部的学生。 1 . 定义学生类
public class Student {private String name;private int age;private ListCourse allCourses; // 定义集合保存全部课程private Student() {this.allCourses new ArrayListCourse();// 实例化List集合}// 通过构造方法设置内容public Student(String name, int age) {// 调用无参构造this();this.setName(name);this.setAge(age);}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public ListCourse getAllCourses() {return allCourses;}public void setAllCourses(ListCourse allCourses) {this.allCourses allCourses;}// 重写toString()方法public String toString() {return 学生姓名 this.name 年龄 this.age;}
}在学生类中存在一个 allCourses 的 List 集合这样在程序运行时一个学生类中可以保存多个 Course 对象。
2 . 定义课程类
public class Course {private String name;private int credit;// 定义集合保存多个学生private ListStudent allStudents;private Course() {// 实例化List集合this.allStudents new ArrayListStudent();}public Course(String name, int credit) {this();this.setName(name);this.setCredit(credit);}public String getName() {return name;}public void setName(String name) {this.name name;}public int getCredit() {return credit;}public void setCredit(int credit) {this.credit credit;}public ListStudent getAllStudents() {return allStudents;}public void setAllStudents(ListStudent allStudents) {this.allStudents allStudents;}// 重写toString()方法public String toString() {return 课程名称 this.name 课程学分 this.credit;}
}课程类与学生类一样都定义了一个 List 集合用于保存多个学生信息。
3 . 测试程序
public class TestMore {public static void main(String[] args) {// 实例化课程对象Course c1 new Course(英语, 3);Course c2 new Course(计算机, 5);// 实例化学生对象Student s1 new Student(张三, 20);Student s2 new Student(李四, 21);Student s3 new Student(王五, 22);Student s4 new Student(赵六, 23);Student s5 new Student(孙七, 24);Student s6 new Student(钱八, 25);// 第一门课程有3个人参加向课程中增加3个学生信息同时向学生中增加课程信息c1.getAllStudents().add(s1);c1.getAllStudents().add(s2);c1.getAllStudents().add(s6);s1.getAllCourses().add(c1);s2.getAllCourses().add(c1);s6.getAllCourses().add(c1);// 第二门课程有6个人参加向课程中增加6个学生信息同时向学生中添加课程信息// 向课程中增加学生信息c2.getAllStudents().add(s1);c2.getAllStudents().add(s2);c2.getAllStudents().add(s3);c2.getAllStudents().add(s4);c2.getAllStudents().add(s5);c2.getAllStudents().add(s6);// 像学生中增加课程信息s1.getAllCourses().add(c2);s2.getAllCourses().add(c2);s3.getAllCourses().add(c2);s4.getAllCourses().add(c2);s5.getAllCourses().add(c2);s6.getAllCourses().add(c2);// 输出一门课程的信息观察一门课程有多少个学生参加System.out.println(c1); // 输出第一门课程IteratorStudent iter1 c1.getAllStudents().iterator();// 迭代输出while (iter1.hasNext()) {Student s iter1.next();System.out.println(\t s);}// 输出一个学生参加的课程信息观察有多少门课程System.out.println(s6);IteratorCourse iter2 s6.getAllCourses().iterator();while (iter2.hasNext()) {// 取得所参加的课程Course c iter2.next();// 输出课程信息System.out.println(\t c);}}
}输出结果如下
课程名称英语课程学分3学生姓名张三年龄20学生姓名李四年龄21学生姓名钱八年龄25
学生姓名钱八年龄25课程名称英语课程学分3课程名称计算机课程学分5程序采用的是双向的处理关系所以学生在选择一个课程时除了课程中要添加学生在学生中也要添加课程信息。在输出课程信息时可以通过课程对象中的集合找到参加此课程的全部学生信息也可以通过学生找到全部参加的课程信息。