首页 技术 正文
技术 2022年11月20日
0 收藏 759 点赞 3,277 浏览 3279 个字

源码分析

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);
} }
}

源码下载

点击下载

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845