Object---clone方法

发布时间 2023-12-12 17:44:13作者: anpeiyong

 

概述

java.lang.Object#clone

 

By convention, the returned object should be obtained by calling {@code super.clone}.
If a class and all of its superclasses (except {@code Object}) obey this convention, it will be the case that {@code x.clone().getClass() == x.getClass()}.

按照约定,应该通过调用super.clone来获得新的克隆对象;

 

 

The method {@code clone} for class {@code Object} performs a specific cloning operation.
First, if the class of this object does not implement the interface {@code Cloneable}, then a {@code CloneNotSupportedException} is thrown.

Note that all arrays are considered to implement the interface {@code Cloneable} and that the return type of the {@code clone} method of an array type {@code T[]} is {@code T[]} where T is any reference or primitive type.

Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned.
Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

clone方法 执行clone操作;

如果 被克隆的对象没有实现Cloneable,将会抛出CloneNotSupportedException;

注意:所有的数组 都被认为实现了Cloneable

clone方法仅创建一个新的对象,对象的字段内容并没有clone;

clone方法是 浅拷贝,不是深拷贝;

 

The class {@code Object} does not itself implement the interface {@code Cloneable}, so calling the {@code clone} method on an object whose class is {@code Object} will result in throwing an exception at run time.

Object类 自身没有实现Cloneable,所以调用Object类的对象的clone将会抛出异常;

 

深拷贝&浅拷贝

浅拷贝

public static void main(String[] args) throws CloneNotSupportedException {
        List<String> list = Arrays.asList("a", "b");

        Person person = new Person("test", list);
        Object clone = person.clone();

        System.out.println(person == clone);


        int[] arr = {1,3,4};
        int[] clone1 = arr.clone();
        System.out.println(arr == clone1);


    }


    static class Person implements Cloneable{

        private String name;
        private List<String> list;

        Person(String name, List<String> list){
            this.name = name;
            this.list = list;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public static void test(){
            System.out.println("test...");
        }

        public List<String> getList() {
            return list;
        }

        public void setList(List<String> list) {
            this.list = list;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

  

 

深拷贝

Person的clone方法深拷贝实现

@Override
        protected Object clone() throws CloneNotSupportedException {
            Person clone = (Person) super.clone();
            ArrayList<String> list = new ArrayList<>(this.list);
            clone.setList(list);
            return clone;
        }