遍历为何报错呀??
遍历学生对象stu课程时为啥会报错呀?
遍历学生对象stu课程时为啥会报错呀?
2018-10-24
private Set<Courses> courses; public Set<Courses> getCourses() { return courses; } public void setCourses(Set<Courses> courses) { this.courses = courses; } //利用foreach遍历student中选择的课程 public void travelStudent(Students student) { for(Courses course:student.getCourses()) { System.out.println("course:"+course.getId()+" , "+course.getName()); } }
构造Student类的时候,需要进行泛型
private Set<Courses> courses; public Set<Courses> getCourses() { return courses; } public void setCourses(Set<Courses> courses) { this.courses = courses; } //利用foreach遍历student中选择的课程 public void travelStudent(Students student) { for(Courses course:student.getCourses()) { System.out.println("course:"+course.getId()+" , "+course.getName()); } } //用于创建新的学生 public Students(String id,String name) { this.id = id; this.name = name; this.courses = new HashSet();//位于util.HashSet类中,是Set接口下的一个重要类 //对courses进行实例化 }
举报