如何在java程序中使用clone方法
我想确切地知道它会给节目带来什么影响?我在网上看到了几个例子,但看不出这个例子是什么 我只知道这一点: 创建与原始对象状态相似的对象的能力。 为此:如何在java程序中使用clone方法,java,Java,我想确切地知道它会给节目带来什么影响?我在网上看到了几个例子,但看不出这个例子是什么 我只知道这一点: 创建与原始对象状态相似的对象的能力。 为此: 机具可克隆 重写clone()方法 在clone()调用super.clone()中将其公开,然后复制任何可变对象的状态 请大家就这个话题举个简单的例子 克隆规则 要克隆的类员工: public class Employee implements Cloneable{ private int empoyeeId; priva
- 机具可克隆
- 重写clone()方法
- 在clone()调用super.clone()中将其公开,然后复制任何可变对象的状态
员工
:
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();
}
//Accessor/mutators methods will go there
}
public class Department
{
private int id;
private String name;
public Department(int id, String name)
{
this.id = id;
this.name = name;
}
//Accessor/mutators methods will go there
}
因此,如果我们需要克隆Employee类,那么我们需要这样做
public class TestCloning {
public static void main(String[] args) throws CloneNotSupportedException
{
Department dept = new Department(1, "Human Resource");
Employee original = new Employee(1, "Admin", dept);
//Lets create a clone of original object
Employee cloned = (Employee) original.clone();
//Let verify using employee id, if cloning actually workded
System.out.println(cloned.getEmpoyeeId());
//Verify JDK's rules
//Must be true and objects must have different memory addresses
System.out.println(original != cloned);
//As we are returning same class; so it should be true
System.out.println(original.getClass() == cloned.getClass());
//Default equals method checks for refernces so it should be false. If we want to make it true,
//we need to override equals method in Employee class.
System.out.println(original.equals(cloned));
}
}
输出:
1
true
true
false
克隆是一个希腊单词,意思是“分支”,指的是从一根树枝上创造出新植物的过程。在生物学中,它是关于复制DNA的 在java中,它本质上意味着能够创建与原始对象状态相似的对象。clone()方法提供此功能。在java中,虽然克隆“旨在”生成同一对象的副本,但不能保证。克隆人有很多如果和但是 默认情况下,java克隆是“逐字段复制”,即对象类不知道将调用clone()方法的类的结构。因此,当JVM被要求进行克隆时,请执行以下操作:
public class Employee implements Cloneable{
private String employeeName;
private Department department;
:
:
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
深度复制深度复制复制所有内容。但是你必须自己去实现它
public class Employee implements Cloneable{
:
@Override
protected Object clone() throws CloneNotSupportedException {
Employee cloned = (Employee)super.clone();
// get the Department object of “this” Employee and make copy of it and assign it to Employee object
cloned.setDepartment((Department)cloned.getDepartment().clone());
return cloned;
}}
}
记住的要点
听起来你已经明白了。缺少什么?@ChrisMartin请在一个简单的例子中转换一下
clone()
很少是正确的解决方案。该行为定义不明确,并且与最终字段有问题。我已经在一个网站上看到了这个例子,我正在寻找一个简单的例子,你可以自己制作
public class Employee implements Cloneable{
private String employeeName;
private Department department;
:
:
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Employee implements Cloneable{
:
@Override
protected Object clone() throws CloneNotSupportedException {
Employee cloned = (Employee)super.clone();
// get the Department object of “this” Employee and make copy of it and assign it to Employee object
cloned.setDepartment((Department)cloned.getDepartment().clone());
return cloned;
}}
}