当我把属性设置为private后,最后那个输出就没了?
package collectionTest;
import java.util.HashSet;
import java.util.Set;
public class Student {
/*
* 学生类
*/
private String id;
private String name;
public Set<Course> course;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//
// public Set<Course> getCourse() {
// return course;
// }
//
// public void setCourse(Set<Course> course) {
// this.course = course;
// }
public Student(String id, String name) {
this.id = id;
this.name = name;
this.course = new HashSet<Course>();
}
}
package collectionTest;
public class Course {
/*
* 课程类
*/
private String id;
private String name;
public Course(String id, String name) {
this.id = id;
this.name = name;
}
public Course() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package collectionTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course> coursesToSelect;
// public List<Course> getCoursesToSelect() {
// return coursesToSelect;
// }
//
// public void setCoursesToSelect(List<Course> coursesToSelect) {
// this.coursesToSelect = coursesToSelect;
// }
public SetTest() {
coursesToSelect = new ArrayList<Course>();
}
public void addCourse() {
// 创建一个课程对象并调用add方法
Course course1 = new Course("2", "数据结构");
coursesToSelect.add(course1);
// Course temp1 = (Course) coursesToSelect.get(0);
// System.out.println("添加了课程:" + temp1.getId() + temp1.getName());
Course course2 = new Course("1", "Java");
coursesToSelect.add(0, course2);
// Course temp2 = (Course) coursesToSelect.get(1);
// System.out.println("添加了课程:" + temp2.getId() + temp2.getName());
Course[] courses = { new Course("3", "JavaScript"), new Course("4", "线性代数") };
coursesToSelect.addAll(Arrays.asList(courses));
// Course temp3 = (Course) coursesToSelect.get(2);
// Course temp4 = (Course) coursesToSelect.get(3);
// System.out.println("添加了课程:" + temp3.getId() + temp3.getName());
}
public void testIterator() {
Iterator<Course> iterator = coursesToSelect.iterator();
// System.out.println("通过迭代器访问!");
// 如果还有元素的话,则为真。
System.out.println("有如下课程可供选择:");
while (iterator.hasNext()) {
Course course = (Course) iterator.next();// 取出具体元素
System.out.println("课程编号:" + course.getId() + " 课程名:" + course.getName());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SetTest setTest = new SetTest();
setTest.addCourse();
setTest.testIterator();
// 创建一个新的学生对象
Student student1 = new Student("1", "小明");
System.out.println("欢迎学生:" + student1.getName() + "来选课!");
// 接收课程ID
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.println("请输入课程ID");
String courseId = input.next();
for (Course course : setTest.coursesToSelect) {
if (course.getName().equals(courseId)) {
student1.course.add(course);
/**
* Set中,添加对象时,无论添加多少次,最终只会保存一个该对象,并且保留的是第一次添加的那个 可以添加空对象
*/
// student1.course.add(course);
}
}
}
setTest.testForeachForSet(student1);
}
// 打印输出学生所选课程
public void testForeachForSet(Student student) {
// 打印输出,学生所选的课程!
for (Course course1 : student.course) {
System.out.println("选择了课程:" + course1.getId() + ":" + course1.getName());
}
}
}