5 つの Employee オブジェクトを含む EmployeeList があるとします。
その EmployeeList のクローンを実行して新しい EmployeeList を作成したいのですが、どうすればよいのでしょうか?
したがって、以下は私のクラスの従業員です。
public class Employee {
private String name;
private String ssn;
private double salary;
private String name() {
return name;
}
private void name(String name) {
this.name = name;
}
private String ssn() {
return ssn;
}
private void ssn(String ssn) {
this.ssn = ssn;
}
private double salary() {
return salary;
}
private void salary(double salary) {
this.salary = salary;
}
void initialize(String initName, String initSsn, double initSalary) {
this.name(initName);
this.ssn(initSsn);
this.salary(initSalary);
}
public Employee(String name, String ssn, double salary) {
this.initialize(name, ssn, salary);
}
public Employee clone() {
return new Employee(this.name, this.ssn, this.salary);
}
}
そして、以下は私のクラスEmployeeListです:
public class EmployeeList implements Cloneable {
private Employee[] list;
private int MAX = 5;
public EmployeeList() {
list = new Employee[MAX];
for (int i = 0; i < MAX; i++)
list[i] = null;
}
public void add(Employee employee) {
list[count] = employee;
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException c) {
System.out.println(c);
return null;
}
}
}
見やすいようにコードを短くしています。
私の問題は次のとおりです。
コピーを実行したとき、元の Employee オブジェクトを指すポインターを使用して EmployeeList をコピーしたと思います。元のオブジェクトを変更すると、新しいリストのオブジェクトも変更されるため
とにかく私はそれを修正することができますか?
どうもありがとうございました。