java中表_Java中数据表的建立

class Student{//学生表

private intstuid;privateString sname;private intage;private StudentCourse studentCourse[];//学生成绩信息

publicStudent(){}public Student(int stuid,String sname,intage){this.stuid =stuid;this.sname =sname;this.age =age;

}public voidsetStudentCourse(StudentCourse[] studentCourse) {this.studentCourse =studentCourse;

}publicStudentCourse[] getStudentCourse() {returnstudentCourse;

}publicString getInfo(){return "学号:" + this.stuid + ",学生姓名:" + this.sname + ",年龄:" + this.age;

}

}classCourse{//课程表privateString name;private intcid;private intcredit;privateStudentCourse studentCourse[];publicCourse(){}public Course(String name,int cid,intcredit){this.cid =cid;this.name =name;this.credit =credit;

}publicStudentCourse[] getStudentCourse() {returnstudentCourse;

}public voidsetStudentCourse(StudentCourse[] studentCourse) {this.studentCourse =studentCourse;

}publicString getInfo(){return "课号:" + this.credit + ",名称:" + this.name + ",学分:" + this.credit;

}

}class StudentCourse{//学生选课表

privateStudent student;privateCourse course;private double score;//成绩

publicStudentCourse(){

}public StudentCourse(Student student,Course course,doublescore){this.course =course;this.score =score;this.student =student;

}publicCourse getCourse() {returncourse;

}publicStudent getStudent() {returnstudent;

}public doublegetScore() {returnscore;

}

}public classMain {public static voidmain(String[] args) {//创建各自的独立对象

Student stu1 = new Student(1,"张三",18);

Student stu2= new Student(1,"李四",20);

Student stu3= new Student(1,"王五",19);

Course ca= new Course("高等数学",1001,5);

Course cb= new Course("线性代数",1002,4);//设置各自的关系//设置学生和课程的关系

stu1.setStudentCourse(newStudentCourse[]{new StudentCourse(stu1,ca,92.5),new StudentCourse(stu1,cb,94.0)

});

stu2.setStudentCourse(newStudentCourse[]{new StudentCourse(stu2,ca,89.0)

});

stu3.setStudentCourse(newStudentCourse[]{new StudentCourse(stu3,cb,95.0),new StudentCourse(stu3,ca,90.5)

});//设置课程和学生的关系

ca.setStudentCourse(newStudentCourse[]{new StudentCourse(stu1,ca,92.5),new StudentCourse(stu2,ca,89.0)new StudentCourse(stu3,ca,90.5)

});

cb.setStudentCourse(newStudentCourse[]{new StudentCourse(stu1,cb,94.0),new StudentCourse(stu3,cb,95.0)

});//找到一门课程,并且输出学习此课程的所有学生的信息及成绩

System.out.println(ca.getInfo());for(int x = 0;x < ca.getStudentCourse().length;x++){

System.out.println("\t"+ca.getStudentCourse()[x].getStudent().getInfo()+",成绩:"+ca.getStudentCourse()[x].getScore());

};

System.out.println("***********");//根据学生输出其信息及选修的课程信息和所得成绩

System.out.println(stu1.getInfo());for(int x = 0;x < stu1.getStudentCourse().length;x++){

System.out.println("\t选修课程:" +stu1.getStudentCourse()[x].getCourse().getInfo()+ ",所得成绩:" +stu1.getStudentCourse()[x].getScore());

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值