class Student{
private int id;
private String name;
private Studentcourse[] studentcourse;
public Student(){
}
public Student(int id,String name){
this.id=id;
this.name=name;
}
public String getInfo(){
return id+" "+name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Studentcourse[] getStudentcourse() {
return studentcourse;
}
public void setStudentcourse(Studentcourse[] studentcourse) {
this.studentcourse = studentcourse;
}
}
class Course{
private int cid;
private String name;
private Studentcourse[] studentcourse;
public Course(){
}
public Course(int cid,String name){
this.cid=cid;
this.name=name;
}
public String getInfo(){
return this.cid+" "+this.name;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Studentcourse[] getStudentcourse() {
return studentcourse;
}
public void setStudentcourse(Studentcourse[] studentcourse) {
this.studentcourse = studentcourse;
}
}
class Studentcourse{
private Student student;
private Course course;
private double score;
public Studentcourse(){
}
public Studentcourse(Student student,Course course,double score){
this.student=student;
this.course=course;
this.score=score;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
public class pratice {
public static void main(String args[]){
Student stu1=new Student(1,"zhangsan");
Student stu2=new Student(2,"lisi");
Student stu3=new Student(3,"wangwu");
Course ca=new Course(1001,"math");
Course cb=new Course(1002,"eng");
stu1.setStudentcourse(new Studentcourse[]{
new Studentcourse(stu1,ca,90.0),
new Studentcourse(stu1,cb,80.0)
});
stu2.setStudentcourse(new Studentcourse[]{
new Studentcourse(stu2,ca,70.0)
});
stu3.setStudentcourse(new Studentcourse[]{
new Studentcourse(stu3,cb,60.0)
});
ca.setStudentcourse(new Studentcourse[]{
new Studentcourse(stu1,ca,90.0),
new Studentcourse(stu2,ca,70.0)
});
cb.setStudentcourse(new Studentcourse[]{
new Studentcourse(stu1,cb,80.0),
new Studentcourse(stu3,cb,60.0)
});
System.out.println(ca.getInfo());
for(int x=0;x<ca.getStudentcourse().length;x++){
System.out.println(ca.getStudentcourse()[x].getStudent().getInfo()+" "+ca.getStudentcourse()[x].getScore());
}
System.out.println(cb.getInfo());
for(int y=0;y<cb.getStudentcourse().length;y++){
System.out.println(cb.getStudentcourse()[y].getStudent().getInfo()+" "+cb.getStudentcourse()[y].getScore());
}
System.out.println("********************************");
System.out.println(stu1.getInfo());
for(int x=0;x<stu1.getStudentcourse().length;x++){
System.out.println(stu1.getStudentcourse()[x].getCourse().getInfo()+" "+stu1.getStudentcourse()[x].getScore());
}
}
}
简单类(多对多练习)
最新推荐文章于 2023-05-24 15:27:45 发布