目前我有这样的代码:对象的防守副本添加到HashSet
public final class Tutor {
private String name;
private final Set<Student> tutees;
public Tutor(String name, Student[] students){
this.name = name;
tutees = new HashSet<Student>();
for (int i = 0; i<students.length; i++)
tutees.add(students[i]);
}
我试图这样,它使/添加,而不是学生的防守副本重写它(只是纸上谈兵)直接将它们添加到HashSet的,我想知道,如果下面的代码将这样做:如果需要的学生
public final class Tutor {
private String name;
private final Set<Student> tutees;
public Tutor(String name, Student[] students){
this.name = name;
tutees = new HashSet<Student>();
for (int i = 0; i<students.length; i++)
tutees.add(students[i](students.getName(), students.getCourse());
}
代码:
public class Student {
private String name;
private String course;
public Student(String name, String course){
this.name = name;
this.course = course;
}
public String getName() { return name; }
public String getCourse() { return course; }
public void setName(String name) {
this.name = name;
}
public void setCourse(String course){
this.course = course;
}
}
谢谢
首先使用编译器。它会发现至少一个错误。接下来,请考虑为学生编写一个构造函数,将Student作为参数 - 封装在Student类中复制学生需要的内容。 –