为什么我的Subject作为成员属性会报错?

为什么我的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()); 
   
 }
}


正在回答

登陆购买课程后可参与讨论,去登陆

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

如果还有什么不明白的地方,可以在问答里继续提问,祝学习愉快~

  • 晚风挽起你长发 提问者 #1
    不是 我是这一句就已经开始报错了 private Subject studentSubject;报错的原因是Subject cannot be resolved to a type
    2018-07-18 19:21:47
  • 晚风挽起你长发 提问者 #2
    我应该在student类里面导入subjcet类的包吗
    2018-07-18 19:26:43
  • 是的,需要导包
    2018-07-19 09:39:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码