public void createStudentAndSelectCourse(){ student = new Student("1","小明"); System.out.println("欢迎学生" + student.name + "选课!"); Scanner console = new Scanner(System.in); for(int i = 0;i<3;i++){ System.out.println("请输入课程ID:"); String courseID = console.next(); for(Course cr:coursesToSelect){ if(cr.id.equals(courseID)){ student.course.add(cr); } } } for(Course cr:student.course){ System.out.println("已选课程:"+ cr.id + ":" + cr.name); } }
其实也可以,但是在testSetContains方法里有这么一句
System.out.println("备选课程中是否包含课程:" + course2.name + ", " +
student.courses.contains(course2));
所以要把student作为SetList类的一个属性,不然退出了createStudentAndSelectCours方法后student对象就不存在了,你在testSetContains方法里也就访问不到