学生类,如下:
public class Student {
//学号 姓名 性别 年龄
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Subject studentSubject;
public Student() {
}
public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.setStudentNo(studentName);
this.setStudentName(studentNo);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
public Student(String studentName, String studentNo, String studentSex, int studentAge, Subject studentSubject) {
this.setStudentNo(studentName);
this.setStudentName(studentNo);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
this.setStudentSubject(studentSubject);
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
if (studentSex.equals("男") || studentSex.equals("女"))
this.studentSex = studentSex;
else
this.studentSex = "无录入";
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
if (studentAge < 10 || studentAge > 100)
this.studentAge = 18;
else
this.studentAge = studentAge;
}
public Subject getStudentSubject() {
return studentSubject;
}
public void setStudentSubject(Subject studentSubject) {
if (this.studentSubject == null)
this.studentSubject = new Subject();
else
this.studentSubject = studentSubject;
}
/* public String introduction(Subject mySubject){
String str = "学生信息如下:\n学号:" + this.getStudentNo() + "\n姓名:" + getStudentName() + "\n性别:" + getStudentSex()
+ "\n年龄:" + getStudentAge() + "\n所报名专业名称: " + mySubject.getSubjectName() + "\n学制年限: "
+ mySubject.getSubjectLife()+"\n专业编号: "+ mySubject.getSubjectNo();
return str;
}*/
public String introduction(){
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + getStudentNo() + "\n性别:" + getStudentSex()
+ "\n年龄:" + getStudentAge() + "\n所报名专业名称: " + this.getStudentSubject().getSubjectName() + "\n学制年限: "
+ this.getStudentSubject().getSubjectLife() +"\n专业编号: "+ this.getStudentSubject().getSubjectNo();
return str;
}
}
学生专业类,如下
public class Subject {
//学科名称 学科编号 学科年限
private String subjectName;
private String subjectNo;
private int subjectLife;
private Student[] myStudents;
private int studentNum;
//无参构造
public Subject() {
}
public Subject(String subjectName, String subjectNo, int subjectLife) {
this.subjectName = subjectName;
this.subjectNo = subjectNo;
this.subjectLife = subjectLife;
}
//带参构造
public Subject(String subjectName, String subjectNo, int subjectLife,Student[] myStudents) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
this.setMyStudents(myStudents);
}
//set get 方法
public String getSubjectName() {
return subjectName;
}
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;
}
//set年限 必须 大于 0
public void setSubjectLife(int subjectLife) {
if (subjectLife<=0)
return;
this.subjectLife = subjectLife;
}
public Student[] getMyStudents() {
return myStudents;
}
public void setMyStudents(Student[] myStudents) {
if (myStudents == null)
this.myStudents = new Student[200];
else
this.myStudents = myStudents;
}
public int getStudentNum() {
return studentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum = studentNum;
}
public String info(){
String str="专业信息如下:\n专业名称:"+this.getSubjectName()+"\n专业编号: "+this.getSubjectNo()+"\n专业年限: "
+this.getSubjectLife();
return str;
}
public void addStudent(Student stu){
int i;
for ( i = 0;i<getMyStudents().length;i++){
if (this.getMyStudents()[i] == null){
stu.setStudentSubject(this);
this.getMyStudents()[i] = stu;
break;
}
}
this.studentNum = i+1;
}
}
测试的代码:
public class SchoolTest {
public static void main(String[] args) {
Subject sub1 = new Subject("计算机与应用","JS001",4);
System.out.println(sub1.info());
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Student s = new Student("js001","小明","男",18,sub1);
System.out.println(s.introduction());
System.out.println("=========================");
System.out.println("该专业学生数:"+s.getStudentSubject().getStudentNum());
}
}
输出结果:
为什么 所报名专业名称: null
学制年限: 0
专业编号: null
这三个数据全部为空呢?