两个clone()方法之间的区别?

时间:2019-05-14 21:35:48

标签: java clone cloneable

//Modified clone() method in Employee class
 @Override
protected Object clone() throws CloneNotSupportedException {
Employee cloned = (Employee)super.clone();
cloned.setDepartment((Department)cloned.getDepartment().clone());//That part  
return cloned;
}

通常clone方法会进行浅表复制,但在代码的上面部分中clone()方法会进行深复制。我被clone()方法卡住了这种区别吗?哪一个复制深,哪个复制浅?谁能解释一下?

这也是Employee类:

public class Employee implements Cloneable{

private int empoyeeId;
private String employeeName;
private Department department;

public Employee(int id, String name, Department dept)
{
    this.empoyeeId = id;
    this.employeeName = name;
    this.department = dept;
}
@Override
protected Object clone() throws CloneNotSupportedException {
    return super.clone();
}

//Getters and Setters
}

还有下属班级:

//Defined clone method in Department class.
@Override
 protected Object clone() throws CloneNotSupportedException {
 return super.clone();
 }

0 个答案:

没有答案