Java类集学习(六)应用范例(一对多的关系)
2015-12-30 21:53
411 查看
使用类集可以表示出以下关系:一个学校可以包含多个学生,一个学生属于一个学校。这是一个典型的一对多的关系。
学生含有的属性:姓名,年龄,对应的学校;
学校的属性:学校名称,对应的学生集合。
分析存储结构图:
先定义一个学生类:
public class Student {
private String name;
private int age;
private School school;//一个学生有一个学校属性
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = 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 School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
// 重写equals方法(list存储的话不需要重写)
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student stu = (Student) obj;
if(this.name.equals(stu.name) && this.age==stu.age){
return true;
}else{
return false;
}
}
// 重写HashCode(list存储的话不需要重写)
public int hashCode(){
return this.name.hashCode()*this.age;
}
// 重写toString
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age;
}
}
定义学校类:
public class School {
private String name;
private List<Student> allStudent;//一个学校对应多个学生
public School(){
this.allStudent = new ArrayList<Student>();//无参构造实例化list集合
}
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;
}
// 重写toString
public String toString(){
return "学校名称:"+this.name;
}
}
测试程序:
public class Demo01 {
public static void main(String[] args) {
// 实例化学校对象
School sch1 = new School("清华大学");
School sch2 = new School("北京邮电大学");
// 实例化学生对象
Student stu1 = new Student("张三",20);
Student stu2 = new Student("李四",21);
Student stu3 = new Student("王五",22);
Student stu4 = new Student("赵六",23);
// 在学校中加入学生
sch1.getAllStudent().add(stu1);
sch1.getAllStudent().add(stu2);
sch2.getAllStudent().add(stu3);
sch2.getAllStudent().add(stu4);
// 一个学生属于一个学校
stu1.setSchool(sch1);
stu2.setSchool(sch1);
stu3.setSchool(sch2);
stu4.setSchool(sch2);
System.out.println(sch1);
Iterator<Student> iter = sch1.getAllStudent().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
System.out.println(sch2);
for(Student students : sch2.getAllStudent()){
System.out.println(students);
}
}
}
输出结果:
学校名称:清华大学
姓名:张三,年龄:20
姓名:李四,年龄:21
学校名称:北京邮电大学
姓名:王五,年龄:22
姓名:赵六,年龄:23
1、分析存储及其关系结构;2、定义类和各个属性,通过两个类中的属性保存彼此引用关系
学生含有的属性:姓名,年龄,对应的学校;
学校的属性:学校名称,对应的学生集合。
分析存储结构图:
先定义一个学生类:
public class Student {
private String name;
private int age;
private School school;//一个学生有一个学校属性
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = 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 School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
// 重写equals方法(list存储的话不需要重写)
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student stu = (Student) obj;
if(this.name.equals(stu.name) && this.age==stu.age){
return true;
}else{
return false;
}
}
// 重写HashCode(list存储的话不需要重写)
public int hashCode(){
return this.name.hashCode()*this.age;
}
// 重写toString
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age;
}
}
定义学校类:
public class School {
private String name;
private List<Student> allStudent;//一个学校对应多个学生
public School(){
this.allStudent = new ArrayList<Student>();//无参构造实例化list集合
}
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;
}
// 重写toString
public String toString(){
return "学校名称:"+this.name;
}
}
测试程序:
public class Demo01 {
public static void main(String[] args) {
// 实例化学校对象
School sch1 = new School("清华大学");
School sch2 = new School("北京邮电大学");
// 实例化学生对象
Student stu1 = new Student("张三",20);
Student stu2 = new Student("李四",21);
Student stu3 = new Student("王五",22);
Student stu4 = new Student("赵六",23);
// 在学校中加入学生
sch1.getAllStudent().add(stu1);
sch1.getAllStudent().add(stu2);
sch2.getAllStudent().add(stu3);
sch2.getAllStudent().add(stu4);
// 一个学生属于一个学校
stu1.setSchool(sch1);
stu2.setSchool(sch1);
stu3.setSchool(sch2);
stu4.setSchool(sch2);
System.out.println(sch1);
Iterator<Student> iter = sch1.getAllStudent().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
System.out.println(sch2);
for(Student students : sch2.getAllStudent()){
System.out.println(students);
}
}
}
输出结果:
学校名称:清华大学
姓名:张三,年龄:20
姓名:李四,年龄:21
学校名称:北京邮电大学
姓名:王五,年龄:22
姓名:赵六,年龄:23
1、分析存储及其关系结构;2、定义类和各个属性,通过两个类中的属性保存彼此引用关系
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树