一只角 2022-04-08 21:00 采纳率: 80%
浏览 54
已结题

关于java封装两个小类一个测试类,运行结果为空。找不到原因

学生类,如下:


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());

    }
}

输出结果:

img


为什么 所报名专业名称: null
学制年限: 0
专业编号: null
这三个数据全部为空呢?

  • 写回答

2条回答 默认 最新

  • Tomshidi 2022-04-08 21:14
    关注

    Student类的setStudentSubject方法改一下

    public void setStudentSubject(Subject studentSubject) {
          this.studentSubject = studentSubject;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月16日
  • 已采纳回答 4月8日
  • 创建了问题 4月8日

悬赏问题

  • ¥15 如何查看h3c 5130s-28f-si交换机配置 及更改网口
  • ¥15 HFSS仿真喇叭天线阵列方向图出现异常
  • ¥15 blender自发光强度
  • ¥15 FIR滤波器算法设计与实现(利用库函数firdec()
  • ¥15 利用单片机产生正弦信号,来测量差分放大电路放大倍数和共模放大倍数的思路和仿真
  • ¥60 关于mavlink的论文咨询
  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥15 中科院研发的TextMind(文心)
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题