为什么我的Subject作为成员属性会报错?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.eros.student; public class Student { // 姓名,学号,性别,年龄,所报专业名称,学制年限,学生信息如下: private String studentname; private String studentnum; private String studentsex; private int age; private Subject studentSubject; public Student() { } public Student(String studentname, String studentnum, String studentsex, int age) { this .setStudentname(studentname); this .setStudentnum(studentnum); this .setStudentsex(studentsex); this .setAge(age); } public String getStudentname() { return studentname; } public void setStudentname(String studentname) { this .studentname = studentname; } public String getStudentnum() { return studentnum; } public void setStudentnum(String studentnum) { this .studentnum = studentnum; } public String getStudentsex() { return studentsex; } public void setStudentsex(String studentsex) { this .studentsex = studentsex; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } public Subject getStudentSubject() { return studentSubject; } public void setStudentSubject(Subject studentSubject) { this .studentSubject = studentSubject; } public String output() { String output = "学生信息如下:\n学生姓名:" + this .getStudentname() + "\n学生学号:" + this .getStudentnum() + "\n学生性别:" + this .getStudentsex() + "\n学生年龄:" + this .getAge() + "\n所报专业名称:" + this .getStudentSubject().getSubjectname() + "\n学制年限:" + this .getStudentSubject().getSubjectnum(); return output; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package com.eros.subject; public class Subject { // 专业名称 专业编号 学制年限,专业信息如下 private String subjectname; private String subjectnum; private int subjecttime; public Subject() { } public Subject(String subjectname, String subjectnum, int subjecttime) { this .setSubjectname(subjectname); this .setSubjectnum(subjectnum); this .setSubjecttime(subjecttime); } public String getSubjectname() { return subjectname; } public void setSubjectname(String subjectname) { this .subjectname = subjectname; } public String getSubjectnum() { return subjectnum; } public void setSubjectnum(String subjectnum) { this .subjectnum = subjectnum; } public int getSubjecttime() { return subjecttime; } public void setSubjecttime( int subjecttime) { this .subjecttime = subjecttime; } /** * 专业信息的方法 * @return 返回专业名称,专业编号,学制年限的信息 */ public String output() { String output = "专业信息如下:\n专业名称:" + this .getSubjectname() + "\n专业编号:" + this .getSubjectnum() + "\n学制年限:" + this .getSubjecttime() + "年" ; return output; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.eros.test; import com.eros.subject.*; import com.eros.student.*; public class Test { public static void main(String[] args) { Subject subject = new Subject( "计算机科学系" , "C00001" , 4 ); System.out.println(subject.output()); System.out.println( "+++++++++++++++++" ); Student student1 = new Student( "张三" , "00001" , "男" , 18 ); System.out.println(student1.output()); } } |
0
收起
正在回答
1回答
Student类中的有参构造方法还应该加入一个Subject对象类型的参数,即
1 | public Student(String studentname, String studentnum, String studentsex, int age,Subject studentSubject) |
然后在Test类中,实例化Student的时候,需要将实例化的Subject对象传递进去,即
1 | Student student1 = new Student( "张三" , "00001" , "男" , 18 ,subject); |
如果还有什么不明白的地方,可以在问答里继续提问,祝学习愉快~
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧