資料表與簡單Java類(多對多)

2021-09-01 03:04:46 字數 3114 閱讀 4182

定義乙個學生選課的操作表:三張資料表

要求:可以實現如下的資訊輸出:

定義出基本類,暫時不考慮所有的關係

class

student

public

student

(int stuid, string name,

int age)

public string getinfo()

}class

course

public

course

(int cid, string name,

int credit)

public string getinfo()

}

乙個學生有多門課,一門課有多個學生,所以應該互相儲存有各自的物件陣列。
class

student

public

student

(int stuid, string name,

int age)

public

void

setcourses

(course[

] courses)

public course[

]getcourses()

public string getinfo()

}class

course

public

course

(int cid, string name,

int credit)

public

void

setstudents

(student[

] students)

public student[

]getstudents()

public string getinfo()

}

學生和每門課程之間都會有乙個成績。現在發現關係表裡面不光有關係字段,還有乙個普通字段,那麼應該在建立乙個類。
class

studentcourse

public

studentcourse

(student student, course course,

double score)

public student getstudent()

public course getcourse()

public

double

getscore()

}

要進行操作的實現
// epi 42  time 15:00

class

student

public

student

(int stuid, string name,

int age)

public

void

setstudentcourse

(studentcourse[

] studentcourses)

public studentcourse[

]getstudentcourses()

public

void

setcourses

(course[

] courses)

public course[

]getcourses()

public string getinfo()

}class

course

public

course

(int cid, string name,

int credit)

public

void

setstudentcourse

(studentcourse[

] studentcourses)

public studentcourse[

]getstudentcourses()

public string getinfo()

}class

studentcourse

public

studentcourse

(student student, course course,

double score)

public student getstudent()

public course getcourse()

public

double

getscore()

}public

class

testdemo);

stu2.

setstudentcourse

(new

studentcourse

);stu3.

setstudentcourse

(new

studentcourse

);//3. 設定課程和學生關係

ca.setstudentcourse

(new

studentcourse

);cb.

setstudentcourse

(new

studentcourse

);//第二部:根據結構取出資料

//1. 可以找到一門課程,以及參加此課程的所有的學生資訊,和他的成績

system.out.

println

(ca.

getinfo()

);for(

int x =

0; x < ca.

getstudentcourses()

.length; x++

) system.out.

println

("********************====");

system.out.

println

(stu1.

getinfo()

);for(

int x =

0; x < stu1.

getstudentcourses()

.length; x++)}

}

這些關係的開發模式必須靈活編寫,隨便轉換。

資料表與簡單java類(多對多)

定義乙個學生選課的操作表 三張表 學生表 編號,姓名,年齡 課程表 課程編號,課程名稱,學分 學生 課程關聯表 學生編號,課程編號,成績。要求可以輸出如下資訊 1 可以找到一門課程參加此課程的所有資訊以及成績 2 根據乙個學生,找到所參加的所有課程和每門課程的成績 第一步 定義出基本類,暫不考慮所有...

資料表多對多

今天學習了資料表多對多,由於有圖理解起來也輕鬆了很多,使用聯合主鍵進行多對多表的關係的管理 中間表 create database auth character set utf8 使用者表 create table users id varchar 32 primary key,name varch...

在MySQL資料庫建立多對多的資料表關係

一 問題 在業務中遇到這樣的情況 我有兩張無關表student和course。student表中的字段是 stu id 和stu name。如圖 course表中的字段是cour id和cour name。如圖 我想要查詢某個同學選了哪些課和某門課被哪些同學選了。這是資料庫中典型的多對多的問題,二 ...