标签:目录 linux 根据 contain 基本 pre png 课堂 edit
要求:定义一个学生选课的操作表:三张数据表
输出要求:
学生成绩单也属于一种关系,暂不考虑,只写基本关系。
class Stu{
private int sid ;
private String sname ;
private int age ;
public Stu(int sid,String sname,int age) {
this.sid = sid ;
this.sname = sname ;
this.age = age ;
}
public String getInfo() {
return "【Student】id = " + this.sid + ",name = " + this.sname +
",age = " + this.age ;
}
}
class Lesson{
private int lid ;
private String lname ;
private double cre ;
public Lesson(int lid,String lname,double cre) {
this.lid = lid ;
this.lname = lname ;
this.cre = cre ;
}
public String getInfo() {
return "【Lesson】id = " + this.lid + ",name = " + this.lname +
",credit = " + this.cre ;
}
}
一个学生有多门课,一门课有多个学生,应该互相保存有各自的对象数组。
private Lesson [] lesson ;
public void setLesson(Lesson [] lesson){
this.lesson = lesson ;
}
public Lesson [] getLesson(){
return this.lesson ;
}
private Stu [] stu ;
public void setStu(Stu [] stu){
this.stu = stu ;
}
public Stu [] getStu(){
return this.stu ;
}
问题来了,学生与每门课程之间都会有一个成绩。在这个关系表里不光有关系字段(学生、课程),还有一个普通字段(成绩),怎么做?
class Stu{
private int sid ;
private String sname ;
private int age ;
//private Lesson [] lesson ; // 添加Score之后,没必要再在lesson中保存stu的对象组
private Score [] score ; // stu与lesson的对应关系通过score描述
public Stu(int sid,String sname,int age) {
this.sid = sid ;
this.sname = sname ;
this.age = age ;
}
public void setScore(Score [] score){
this.score = score ;
}
public Score [] getScore(){
return this.score ;
}
public String getInfo() {
return "【Student】id = " + this.sid + ",name = " + this.sname +
",age = " + this.age ;
}
}
class Lesson{
private int lid ;
private String lname ;
private double cre ;
// private Stu [] stu ; // 添加Score之后,没必要再在lesson中保存stu的对象组
private Score [] score ; // stu与lesson的对应关系通过score描述
public Lesson(int lid,String lname,double cre) {
this.lid = lid ;
this.lname = lname ;
this.cre = cre ;
}
public void setScore(Score [] score){
this.score = score ;
}
public Score [] getScore(){
return this.score ;
}
public String getInfo() {
return "【Lesson】id = " + this.lid + ",name = " + this.lname +
",credit = " + this.cre ;
}
}
class Score{
private Stu stu ;
private Lesson lesson ;
private double score ;
public Score(Stu stu,Lesson lesson,double score){
this.stu = stu ;
this.lesson = lesson ;
this.score = score ;
}
public Stu getStu(){
return this.stu ;
}
public Lesson getLesson(){
return this.lesson ;
}
public double getScore(){
return this.score ;
}
public String getInfo(){
return "【Score】stu = " + this.stu + ",lesson = " + this.lesson +
",score = " + this.score ;
}
}
class Stu{
private int sid ;
private String sname ;
private int age ;
//private Lesson [] lesson ; // 添加Score之后,没必要再在lesson中保存stu的对象组
private Score [] score ; // stu与lesson的对应关系通过score描述
public Stu(int sid,String sname,int age) {
this.sid = sid ;
this.sname = sname ;
this.age = age ;
}
public void setScore(Score [] score){
this.score = score ;
}
public Score [] getScore(){
return this.score ;
}
public String getInfo() {
return "【Student】id = " + this.sid + ",name = " + this.sname +
",age = " + this.age ;
}
}
class Lesson{
private int lid ;
private String lname ;
private double cre ;
// private Stu [] stu ; // 添加Score之后,没必要再在lesson中保存stu的对象组
private Score [] score ; // stu与lesson的对应关系通过score描述
public Lesson(int lid,String lname,double cre) {
this.lid = lid ;
this.lname = lname ;
this.cre = cre ;
}
public void setScore(Score [] score){
this.score = score ;
}
public Score [] getScore(){
return this.score ;
}
public String getInfo() {
return "【Lesson】id = " + this.lid + ",name = " + this.lname +
",credit = " + this.cre ;
}
}
class Score{
private Stu stu ;
private Lesson lesson ;
private double score ;
public Score(Stu stu,Lesson lesson,double score){
this.stu = stu ;
this.lesson = lesson ;
this.score = score ;
}
public Stu getStu(){
return this.stu ;
}
public Lesson getLesson(){
return this.lesson ;
}
public double getScore(){
return this.score ;
}
public String getInfo(){
return "【Score】stu = " + this.stu + ",lesson = " + this.lesson +
",score = " + this.score ;
}
}
public class TestDemo { // 设置开发需求
public static void main(String args[]) {
// 第一步:设置类对象间的关系
// 1、分别创建各类的实例化对象
Stu stua = new Stu(1313,"Dexter",17) ;
Stu stub = new Stu(1728,"Tsukishima Kei",18) ;
Stu stuc = new Stu(1230,"Toono Takaki",19) ;
Lesson lesa = new Lesson(101,"Linux",3.0) ;
Lesson lesb = new Lesson(104,"MySql",2.5) ;
// 2、设置对象间的引用关系
// 2.1 设置学生与课程的关系,并且需要带上成绩
stua.setScore(new Score [] { new Score(stua,lesa,95.0),new Score(stua,lesb,89.5) }) ; // stua学了lesa、lesb,分别95.0、89.5分
stub.setScore(new Score [] { new Score(stub,lesa,87.0) }) ; // stub学了lesa,87.0分
stuc.setScore(new Score [] { new Score(stuc,lesb,85.5) }) ; // stuc学了lesb,85.5分
// 2.2 设置课程与学生的关系
lesa.setScore(new Score [] { new Score(stua,lesa,95.0),new Score(stub,lesa,87.0) }) ; // lesa有stua和stub学,分别95.0、87.0分
lesb.setScore(new Score [] { new Score(stua,lesb,89.5),new Score(stuc,lesb,85.5) }) ; // lesb有stua和stuc学,分别89.5、85.5分
// 第二步:根据关系取出数据
// 3、找到一门课程,参加此课程的所有的学生信息,还有每人的成绩
System.out.println(lesa.getInfo()) ; // 输出课程的信息
for(int x= 0 ; x < lesa.getScore().length ; x++){ // 输出课程对应所有学生的信息、成绩
System.out.println("\t##" + lesa.getScore()[x].getStu().getInfo() +
",score = " + lesa.getScore()[x].getScore()) ;
}
System.out.println("==========================================================================") ;
// 4、可以根据一个学生找到学生对应的课程信息、以及每门课的成绩
System.out.println(stua.getInfo()) ; // 输出学生的信息
for(int x= 0 ; x < stua.getScore().length ; x++){ // 输出学生对应所有课程的信息、成绩
System.out.println("\t##" + stua.getScore()[x].getLesson().getInfo() +
",score = " + stua.getScore()[x].getScore()) ;
}
}
}
阿里云【名师课堂】Java面向对象开发42:【第03个代码模型】综合案例:数据表与简单Java类(多对多)
标签:目录 linux 根据 contain 基本 pre png 课堂 edit
原文地址:https://www.cnblogs.com/playerone/p/13093223.html