源码分析
Course.java
package com.ftl.many2many; import java.util.*; public class Course
{
private int credit;
private String name;
private List<Student> allStudent;
public int getCredit()
{
return credit;
}
public void setCredit(int credit)
{
this.credit = credit;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<Student> getAllStudent()
{
return allStudent;
}
public void setAllStudent(List<Student> allStudent)
{
this.allStudent = allStudent;
}
public Course()
{
this.allStudent = new ArrayList<Student>();
}
public Course(String name, int credit)
{
this();
this.setCredit(credit);
this.setName(name);
} public String toString()
{
return "课程名称:" + this.name + "\t课程学分:" + this.credit;
}
}
School.java
package com.ftl.many2many; import java.io.*;
import java.util.*;
public class School
{
private String name;
private List<Student> allStudent;
public School()
{
this.allStudent = new ArrayList<Student>();
}
public School(String name)
{
this();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getAllStudent() {
return allStudent;
}
public void setAllStudent(List<Student> allStudent) {
this.allStudent = allStudent;
}
public String toString()
{
return "学校姓名" + this.name;
} }
Student.java
package com.ftl.many2many; import java.util.*;
public class Student
{
private int age;
private String name;
private School school;
private List<Course> allCourse; public Student()
{
this.allCourse = new ArrayList<Course>();
}
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 School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Course> getAllCourse() {
return allCourse;
}
public void setAllCourse(List<Course> allCourse) {
this.allCourse = allCourse;
}
public String toString()
{
return "学生姓名:" + this.name + "\t 年龄:" + this.age;
}
}
testDemo.java
package com.ftl.many2many; import java.util.Iterator; public class testDemo
{
public static void main(String[] args)
{
School sch = new School("海风大学");
Student s1 = new Student("张三", 12);
Student s2 = new Student("赵四", 22);
Student s3 = new Student("张5", 11);
Course c1 = new Course("计算机", 3);
Course c2 = new Course("语文", 1);
Course c3 = new Course("数学", 2);
//3个学生一个学校
s1.setSchool(sch);
s2.setSchool(sch);
s3.setSchool(sch);
//一个学校3个学生
sch.getAllStudent().add(s1);
sch.getAllStudent().add(s2);
sch.getAllStudent().add(s3);
//第一门课3个xues
c1.getAllStudent().add(s3);
c1.getAllStudent().add(s2);
c1.getAllStudent().add(s1);
s2.getAllCourse().add(c2);
s1.getAllCourse().add(c1);
s3.getAllCourse().add(c3); //第二门一个学生
c2.getAllStudent().add(s3);
s3.getAllCourse().add(c2);
c3.getAllStudent().add(s3);
s3.getAllCourse().add(c3);
//输出一门课信息,观察一门课多少学生:
System.out.println(c1);
Iterator<Student> iter = null;
iter = c1.getAllStudent().iterator();
System.out.println("C1 选课情况 : ");
while(iter.hasNext())
{
Student c = iter.next();
System.out.println("\t|-" + c);
}
System.out.println("----------------------------");
System.out.println("学校学生情况: ");
iter = sch.getAllStudent().iterator();
while(iter.hasNext())
{
Student stu = (Student) iter.next();
System.out.println("\t|-" + stu);
}
System.out.println("----------------------------");
//张5的选课情况:
System.out.println("学生张武选课情况: ");
System.out.println(s3);
Iterator<Course> it = s3.getAllCourse().iterator();
while(it.hasNext())
{
Course c = it.next();
System.out.println("\t|-" + c);
} }
}