一对多关系!!!
package 一对多关系;
public class Student { //定义学生类
private String name ; //声明姓名属性
private int age ; //声明年龄属性
private School school ; //一个学生属于一个学校
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 ;
}
public String toString(){ //覆写toString()
return "姓名:" + this.name + " 年龄:" + this.age ;
}
}
package 一对多关系;import java.util.ArrayList;
import java.util.List;
public class School {
private String name ;
private List<Student> allStudent ; //类集存储学生,一个学校有多个学生
public School(){
this.allStudent = new ArrayList<Student>() ; //实例化List集合
}
public School(String name){
this() ; //调用无参构造方法
this.setName(name) ;
}
public String getName(){
return name ;
}
public void setName(String name){
this.name = name ;
}
public List<Student> getAllStudent(){ //取得全部学生
return allStudent ;
}
public String toString(){ //返回学校名称
return "学校名称:" + this.name ;
}
}
package 一对多关系;import java.util.Iterator;
public class TextDemo {
public static void main(String args[]){
School sch = new School("兰州石化大学") ; //实例化学校
Student stu1 = new Student("张三" , 21) ; //实例化学生类
Student stu2 = new Student("李四" , 22) ; //实例化学生类
Student stu3 = new Student("阿菜" , 20) ; //实例化学生类
sch.getAllStudent().add(stu1) ; //学校中加入学生
sch.getAllStudent().add(stu2) ; //学校中加入学生
sch.getAllStudent().add(stu3) ; //学校中加入学生
stu1.setSchool(sch) ; //一个学生属于一个学校
stu2.setSchool(sch) ; //一t个学生属于一个学校
stu2.setSchool(sch) ; //一个学生属于一个学校
System.out.println(sch) ;
//下面输出学生,是类集,则需要Iterator类迭代输出
Iterator<Student> iter = sch.getAllStudent().iterator() ;
while(iter.hasNext()
){
System.out.println(iter.next());
}
}
}
多对多关系!!!
package 多对多关系;
import java.util.ArrayList;
import java.util.List;
public class Student {
private String name ;
private int age ;
private List<Count> allCount ;
public Student(){
this.allCount = new ArrayList<Count>() ;
}
public Student(String name , int age ){
this() ;
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 List<Count> getCount(){
return allCount ;
}
public void setSchool(List<Count> allCount){
this.allCount = allCount ;
}
public String toString(){
return "姓名:" + this.name + " 年龄:" + this.age ;
}
}
package 多对多关系;
import java.util.ArrayList;
import java.util.List;
public class Count {
private String name ;
private float credit ;
private List<Student> allStudent ; //一门课程有多个学生
public Count(){
this.allStudent = new ArrayList<Student>() ;
}
public Count(String name , float credit){
this() ;
this.name = name ;
this.credit = credit ;
}
public String getName(){
return name ;
}
public void setName(String name){
this.name = name ;
}
public float getCredit(){
return credit ;
}
public void setCredit(float credit){
this.credit = credit ;
}
public List<Student> getAllStudent(){
return allStudent ;
}
public void setAllStudent(List<Student> allStudent){
this.allStudent = allStudent ;
}
public String toString(){
return "课程名称:" + this.name + " 课程成绩:" + this.credit ;
}
}
package 多对多关系;
import java.util.Iterator;
import 多对多关系.Student;
import 多对多关系.Count;
public class TextDemo {
public static void main(String args[]){
Count c1 = new Count("java基础" , 30 ) ;
Count c2 = new Count("计算机基础" , 25 ) ;
Student stu1 = new Student("张三" , 21) ; //实例化学生类
Student stu2 = new Student("李四" , 22) ; //实例化学生类
Student stu3 = new Student("阿菜" , 20) ; //实例化学生类
c1.getAllStudent().add(stu1) ;
c1.getAllStudent().add(stu2) ;
c1.getAllStudent().add(stu3) ;
c2.getAllStudent().add(stu1) ;
c2.getAllStudent().add(stu2) ;
c2.getAllStudent().add(stu3) ;
stu1.getCount().add(c1) ;
stu1.getCount().add(c2) ;
stu2.getCount().add(c1) ;
stu2.getCount().add(c2) ;
stu3.getCount().add(c1) ;
stu3.getCount().add(c2) ;
System.out.println(c1);
Iterator<Student> iter = c1.getAllStudent().iterator() ;
while(iter.hasNext()){
Student s = iter.next() ;
System.out.println(s) ;
}
System.out.println(c2);
Iterator<Student> iters = c2.getAllStudent().iterator() ;
while(iters.hasNext()){
Student s = iters.next() ;
System.out.println(s) ;
}
}
}
|
|