码迷,mamicode.com
首页 > 其他好文 > 详细

初学杂文 数据表映射

时间:2015-12-20 11:40:24      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!