🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 1、编写并测试 Subject 类 ``` public class Subject { private String name; // 学科名称 private String code; // 学科编号 private int year; // 学制年限 public Subject() {} public Subject(String name, String code, int age) { this.setName(name); this.setCode(code); this.setAge(age); } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getCode() { return this.code; } public void setCode(String code) { this.code = code; } public int getYear() { return this.year; } public void setYear(int year) { this.year = year; } public void info() { System.out.println("专业信息如下:"); System.out.println("专业名称:" + this.getName()); System.out.println("专业编号:" + this.getCode()); System.out.println("学制年限:" + this.getYear() + "年"); } } ``` ## 2、编写并测试 Student 类 ``` public class Student { private String code; // 学号 private String name; // 姓名 private String gender; // 性别 private int age; // 年龄 public Student() {} public Student(String code, String name, String gender, int age) { this.setCode(code); this.setName(name); this.setGender(gender); this.setAge(age); } public String getCode() { return this.code; } public void setCode(String code) { this.code = code; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getGender() { return this.gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public void introduction() { System.out.println("学生信息如下:"); System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getCode()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); } } ``` ## 3、通过方法实现学生与专业关联 * 方式一 ``` public class Student { public void introduction(String name, int year) { System.out.println("学生信息如下:"); System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getCode()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); System.out.println("所报专业名称:" + name); System.out.println("学制年限:" + year + "年"); } } ``` * 方式二 ``` public class Student { public void introduction(Subject subject) { System.out.println("学生信息如下:"); System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getCode()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); System.out.println(subject); } } ``` * 方式三 ``` public class Student { // ... private Subject subject; public Subject getSubject() { return subject; } public void setSubject(Subject subject) { this.subject = subject; } public void introduction() { System.out.println("学生信息如下:"); System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getCode()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); System.out.println(this.getSubject()); } } ``` ## 4、完成学生信息存储 ``` public class Subject { private Student[] students = new Student[200]; // 报名选修的学生信息 private int count; // 报名选修的学生个数 public Subject(String name, String code, int age, Student[] students) { this.setName(name); this.setCode(code); this.setAge(age); this.setStudents(students); } public Student[] getStudents() { return this.students; } public void setStudents(Student[] students) { this.students = students; } public int getCount() { return this.count; } public void setCount(int count) { this.count = count; } /** * 专业添加学生的同时需要给学生设置专业属性 */ public void addStudent(Student student) { for (int i = 0; i < this.getStudents().length; i++) { if (this.getStudents()[i] == null) { student.setSubject(this); this.getStudents()[i] = student; this.setCount(i + 1); break; } } } } ```