我正在尝试克隆一个DTO。 我已经采取了一个DTO对象,如下所示:
public class Employee implements Cloneable
{
String name;
String dept;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
但是这行代码报错了:
public class Test
{
public static void main(String args[]) {
Employee emp1 = new Employee();
emp1.setDept("10");
emp1.setName("Kiran");
Employee emp2 = (Employee) emp1.clone(); // This Line is giving error .
}
}
我的问题是,克隆方法来自于Object类,为什么我们不能像使用toString方法一样直接使用它?