我想克隆DTO。我已经采取了DTO对象,如下所示:在Java中克隆对象
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方法?