我理解什么是引用泄漏并且可以处理它,但是当我不得不编写一个复制构造函数时,我很困惑。复制构造函数是否也会导致泄漏?
public class Student {
private final int ID;
private final String studentName;
private double[] Grades = new double[5];
// constructor:
public Student(int ID, String Name, double[] quizGrades) {
this.ID = ID;
this.studentName = Name;
this.Grades = new double[quizGrades.length]; // use new to avoid REFERENCE LEAKS...
for (int k = 0; k < quizGrades.length; k++) //
this.Grades[k] = quizGrades[k]; //
}
// Copy constructor:
public Student(Student stu) {
this(stu.ID, stu.studentName, stu.Grades);
}
}
我制作了一个数组quizGrades
的副本,所以不会有泄漏,但我不确定复制构造函数是否正常。