标签:
class Teacher{
private String name;
private Student student []; //多个学生用对象数组
public Teacher(String name) {
this.name = name ;
}
public void setStudent(Student [] student){ //设置学生时候
this.student = student;
}
public String getInfo(){
return "Name :" + name ;
}
public Student [] getStudent(){ //返回的是一个数组
return this.student ;
}
}
class Student{
private String name;
private int ID;
private Teacher teacher ;
public Student (String name,int ID){
this.name = name;
this.ID = ID;
}
public void setTeacher (Teacher teacher){
this.teacher = teacher ;
}
public String getInfo(){
return "Name :" + this.name +", ID:" + this.ID ;
}
public Teacher getTeacher (){
return this.teacher ;
}
}
public class Test{
public static void main(String args[]){
Student stu1 = new Student("zhang", 001);
Student stu2 = new Student("zhuo", 002);
Student stu3 = new Student("peng", 003) ;
Teacher tea1 = new Teacher("Java") ;
stu1.setTeacher(tea1) ;
stu2.setTeacher(tea1) ;
stu3.setTeacher(tea1) ;
tea1.setStudent(new Student [] {stu1,stu2,stu3}) ; //设置老师的学生,传入的是数组
System.out.println(stu1.getInfo());
System.out.println(stu1.getTeacher().getInfo()) ;
for(int i = 0; i < tea1.getStudent().length;i++ ) //获取数组的长度
System.out.println(tea1.getStudent()[i].getInfo());
}
}
标签:
原文地址:http://www.cnblogs.com/da-peng/p/5060408.html