类集中多对多关系案例

实例要求

一个学生可以选多门课程, 门课程可以有多个学生参加,那么这就是一个典型的多对多关系。

要完成本程序,首先应该定义两个类:学生信息类Student、课程信息类Course,在一个学生类中存在一个集合,保存全部的课程,而在课程类中也要存在一个集合,保存全部的学生。

实例主要采用的知识

1、List 集合

2、引用传递

3、Iteratro

学生类:

import java.util.List ;
import java.util.ArrayList ;
public class Student{
	private String name ;
	private int age ;
	private List<Course> allCourses ;
	public Student(){
		this.allCourses = new ArrayList<Course>() ;
	}
	public Student(String name,int age){
		this() ;
		this.name = name ;
		this.age = age ;
	}
	public List<Course> getAllCourses(){
		return this.allCourses ;
	}
	public void setName(String name){
		this.name = name ;
	}
	public void setAge(int age){
		this.age = age ;
	}
	public String getName(){
		return this.name ;
	}
	public int getAge(){
		return this.age ;
	}
	public String toString(){
		return "学生姓名:" + this.name + ";年龄:" + this.age ;
	}
};

课程类:

import java.util.List ;
import java.util.ArrayList ;
public class Course{
	private String name ;
	private int credit ;
	private List<Student> allStudents ;
	public Course(){
		this.allStudents = new ArrayList<Student>() ;
	}
	public Course(String name,int credit){
		this() ;
		this.name = name ;
		this.credit = credit ;
	}
	public List<Student> getAllStudents(){
		return this.allStudents ;
	}
	public void setName(String name){
		this.name = name  ;
	}
	public void setCredit(int credit){
		this.credit = credit ;
	}
	public String getName(){
		return this.name ;
	}
	public int getCredit(){
		return this.credit ;
	}
	public String toString(){
		return "课程名称:" + this.name + ";课程学分:" + this.credit ;
	}
};
关系测试类:

import java.util.Iterator ;
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("钱八",24) ;
		// 第一门课程有三个学生参加
		c1.getAllStudents().add(s1) ;
		c1.getAllStudents().add(s2) ;
		c1.getAllStudents().add(s6) ;
		s1.getAllCourses().add(c1) ;
		s2.getAllCourses().add(c1) ;
		s6.getAllCourses().add(c1) ;
		// 第二门课程有六个学生参加
		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) ;
		Iterator<Student> iter1 = c1.getAllStudents().iterator() ;
		while(iter1.hasNext()){
			Student s = iter1.next() ;
			System.out.println("\t|- " + s) ;
		}
		// 通过学生找到学生参加的课程
		System.out.println(s6) ;
		Iterator<Course> iter2 = s6.getAllCourses().iterator() ;
		while(iter2.hasNext()){
			Course c = iter2.next() ;
			System.out.println("\t|- " + c) ;
		}
	}
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值