总共三种方法在代码中标出。
学生类:
1 package com.imooc.model; 2 3 public class Student { 4 // 学号、姓名、性别、年龄 5 private String studentNo; 6 private String studentName; 7 private String studentSex; 8 private int studentAge; 9 private Subject studentSubject; 10 11 public Student() { 12 } 13 14 public Student(String studentNo, String studentName, String studentSex, int studentAge,Subject studentSubject) { 15 16 this.setStudentNo(studentNo); 17 this.setStudentName(studentName); 18 this.setStudentSex(studentSex); 19 this.setStudentAge(studentAge); 20 this.setStudentSubject(studentSubject); 21 22 } 23 24 public Student(String studentNo, String studentName, String studentSex, int studentAge) { 25 26 this.setStudentNo(studentNo); 27 this.setStudentName(studentName); 28 this.setStudentSex(studentSex); 29 this.setStudentAge(studentAge); 30 31 } 32 33 public String getStudentNo() { 34 return studentNo; 35 } 36 37 public void setStudentNo(String studentNo) { 38 this.studentNo = studentNo; 39 } 40 41 public String getStudentName() { 42 return studentName; 43 } 44 45 public void setStudentName(String studentName) { 46 this.studentName = studentName; 47 } 48 49 public String getStudentSex() { 50 return studentSex; 51 } 52 53 public void setStudentSex(String studentSex) { 54 this.studentSex = studentSex; 55 } 56 57 public int getStudentAge() { 58 return studentAge; 59 } 60 61 public void setStudentAge(int studentAge) { 62 if (studentAge < 10 || studentAge > 100) 63 this.studentAge = 18; 64 else 65 this.studentAge = studentAge; 66 } 67 /** 68 * 获取专业对象,如果没有实例化,先实例化 69 * @return 70 */ 71 public Subject getStudentSubject() { 72 if(this.studentSubject==null) 73 this.studentSubject=new Subject(); 74 return studentSubject; 75 } 76 77 public void setStudentSubject(Subject studentSubject) { 78 this.studentSubject = studentSubject; 79 } 80 81 /** 82 * 方法3:在类中添加专业对象作为属性,通过其属性获得相关信息 83 * 关联性强 84 * @return 85 */ 86 public String introduction() { 87 String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号" + this.getStudentNo() + "\n性别:" 88 + this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n专业名称:" + this.getStudentSubject().getSubjectName()+ "\n年限:" 89 + this.getStudentSubject().getSubjectLife(); 90 return str; 91 } 92 /** 93 * 方法1:在方法中添加两个参数,分别表示专业名称和学制年限 94 * 容易理解,参数列表长 95 * @param subjectName 96 * @param subjectLife 97 * @return 98 */ 99 public String introduction(String subjectName, int subjectLife) { 100 String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号" + this.getStudentNo() + "\n性别:" 101 + this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n专业名称:" + subjectName + "\n年限:" 102 + subjectLife; 103 return str; 104 } 105 /** 106 * 方法2:在方法中添加一个专业对象作为参数,通过其属性获得相关信息 107 * 更加简单,获取参数方便 108 * @param mySubject 所选专业方法 109 * @return 110 */ 111 public String introduction(Subject mySubject){ 112 String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号" + this.getStudentNo() + "\n性别:" 113 + this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n专业名称:" + mySubject.getSubjectName() + "\n年限:" 114 + mySubject.getSubjectLife(); 115 return str; 116 } 117 }
专业类:
package com.imooc.model; public class Subject { // 成员属性:学科名称、编号、年限 private String subjectName; private String subjectNo; private int subjectLife; // 无参构造方法 public Subject() { } // 带参构造 public Subject(String subjectName, String subjectNo, int subjectLife) { this.setSubjectName(subjectName); this.setSubjectNo(subjectNo); this.setSubjectLife(subjectLife); } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } public String getSubjectNo() { return subjectNo; } public void setSubjectNo(String subjectNo) { this.subjectNo = subjectNo; } public int getSubjectLife() { return subjectLife; } // 设置学制年限 public void setSubjectLife(int subjectLife) { if (subjectLife <= 0) return; this.subjectLife = subjectLife; } public String getSubjectName() { return subjectName; } /** * 专业介绍的方法 * @return 专业介绍的相关信息 */ public String info() { String str = "专业信息如下:\n专业名称:\n" + this.getSubjectName() + "\n专业编号:" + this.getSubjectNo() + "\n学制年限:" + this.getSubjectLife()+"年"; return str; } }
test:
package com.imooc.text; import com.imooc.model.*; public class SchoolTest { public static void main(String[] args){ Subject sub1=new Subject("计算机科学与应用","J0001",4); System.out.println(sub1.info()); System.out.println("==================================="); Student stu1=new Student("S01","张三","男",18,sub1); System.out.println(stu1.introduction()); System.out.println("==================================="); Student stu2=new Student("S02","李四","女",17); System.out.println(stu2.introduction("计算机科学与应用",4)); System.out.println("==================================="); Student stu3=new Student("S03","王五","男",18); System.out.println(stu3.introduction(sub1)); } }