public void testForEachForSet(Student student) {
// 打印输出,学生所选的课程!
System.out.println("共选择了:" + student.courses.size() + "门课程!");
for(Course cr:student.courses) {
System.out.println("选择了课程:" + cr.id + ":" + cr.name);haha,楼主的 Student 的 s 没有大写,
很明显,courses 是 Student类的 属性。
package com.imooc.stu;
import java.util.*;
public class Student {
public String id;
public String name;
public Set<Course> courses;
public Student(String id, String name) {
this.id = id;
this.name = name;
this.courses = new HashSet<Course>();
}
}Stundet类定义泛型 public Set <Course> courses;
看看你Student中Set的类型和泛型集合类型有没有写错。
student.courses;中的courses是否与Student类中一致。
可能你测试类中创建的List没用泛型吧public List<Course> courses;