如何在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被要求进行克隆时,请执行以下操作:

  • 如果类只有基元数据类型成员,则将创建对象的全新副本,并返回对新对象副本的引用
  • 如果类包含任何类类型的成员,则仅复制对这些成员的对象引用,因此原始对象和克隆对象中的成员引用都引用同一对象
  • 除了上述默认行为之外,您始终可以覆盖此行为并指定自己的行为

    根据java文档(格式化和解压缩)

    浅复制通常克隆对象的方法,创建同一类的新实例,并将所有字段复制到新实例并返回。这只是肤浅的复制品。其中包含的对象将不会复制到克隆对象。示例-

    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;
    }}
    }
    
    记住的要点

  • 当您不知道是否可以调用 特定的类,因为您不确定它是否在该类中实现 类,您可以通过检查该类是否为的实例进行检查 “可克隆”界面如下所示

    if(可克隆的obj1实例){ obj2=obj1.clone(); }

  • 因为Cloneabe接口是一个标记接口(没有任何方法 在它中),您永远不能将对象强制转换为Cloneable并调用clone() 方法。检查下面

    //不要这样做。克隆人没有任何方法 obj2=(可克隆)obj1.clone()

  • 没有对正在克隆的对象调用构造函数。所以,你必须 确保所有成员都已正确设置

  • 如果类有final字段,则不能在 克隆方法

  • 如果您的类是singleton,则必须负责克隆

    公共对象克隆()引发CloneNotSupportedException{ 抛出新的CloneNotSupportedException(); }


  • 听起来你已经明白了。缺少什么?@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;
    }}
    }