美文网首页
Java 浅克隆、深克隆

Java 浅克隆、深克隆

作者: 魏树鑫 | 来源:发表于2019-06-01 20:39 被阅读0次

    浅克隆

    只复制了基本数据类型和String数据类型以及对应的数组类型,其他引用数据类型只是复制了引用地址;

    使用方式

    实现Cloneable接口,然后重写clone方法,调用super.clone()即可

    public static class Person implements Cloneable {
        @Override
        protected Person clone() throws CloneNotSupportedException {
             return (Person) super.clone();
        }
    }
    

    例如

    public static class Person implements Cloneable {
        public String name;
        public int age;
        public String[] names;
        public Baby baby;
        public ArrayList<String> names2 = new ArrayList<>();
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person{" + "name='" + name + '\'' + ", age=" + age + ", names=" + Arrays.toString(names) + ", baby=" + baby + ", names2=" + names2 + '}';
        }
        @Override
        protected Person clone() throws CloneNotSupportedException {
            return (Person) super.clone();
        }
    }
    public static class Baby implements Cloneable {
        public String name;
        public int age;
        public String[] names;
        public Baby(String name, int age, String[] names) {
            this.name = name;
            this.age = age;
            this.names = names;
        }
        @Override
        public String toString() {
            return "Baby{" + "name='" + name + '\'' + ", age=" + age + ", names=" + Arrays.toString(names) + '}';
        }
        @Override
        protected Baby clone() throws CloneNotSupportedException {
            return (Baby) super.clone();
        }
    }
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("person", 10);
        person.names = new String[]{"qq", "www"};
        person.baby = new Baby("baby", 20, new String[]{"qqqqq", "wwwww"});
        person.names2.add("1111");
        person.names2.add("2222");
        //  对克隆后的数据进行更改
        Person clone = person.clone();
        clone.name = "person1";
        clone.age = 23;
        clone.names2.add("3333");
        clone.names = new String[]{"111", "222"};
    
        clone.baby.name = "baby1";
        clone.baby.names = new String[]{"ttttt"};
        
        System.out.println(person.toString());
        System.out.println(clone.toString());
    }
    
    

    对应输出

    Person{name='person', age=10, names=[qq, www], baby=Baby{name='baby1', age=20, names=[ttttt]}, names2=[1111, 2222, 3333]}
    Person{name='person1', age=23, names=[111, 222], baby=Baby{name='baby1', age=20, names=[ttttt]}, names2=[1111, 2222, 3333]}
    

    结论

    发现,在对克隆的数据进行数据更改后:

    • 基本数据类型,String,基本数据类型和String对应的数组,复制了一份,更改不会影响原始数据;
    • 引用类型数据,只是复制了引用地址,对克隆后的引用数据类型操作,原始数据也会变,如测试中的Baby对象;

    深克隆

    对引用数据类型创建新的对象,并将属性克隆过去;需要递归克隆引用数据类型;

    实现方式

    public static class Person implements Cloneable {
        @Override
        protected Person clone() throws CloneNotSupportedException {
            Person person = (Person) super.clone();
            person.baby = baby.clone();
            person.names2 = (ArrayList<String>) names2.clone();
            return person;
        }
    }
    

    例如

    将上面浅克隆的例子中的clone方法改成

    public static class Person implements Cloneable {
        @Override
        protected Person clone() throws CloneNotSupportedException {
            Person person = (Person) super.clone();
            person.baby = baby.clone();
            person.names2 = (ArrayList<String>) names2.clone();
            return person;
        }
    }
    

    输出

    Person{name='person', age=10, names=[qq, www], baby=Baby{name='baby', age=20, names=[qqqqq, wwwww]}, names2=[1111, 2222]}
    Person{name='person1', age=23, names=[111, 222], baby=Baby{name='baby1', age=20, names=[ttttt]}, names2=[1111, 2222, 3333]}
    

    结论

    • 引用数据类型创建了一份新的数据,对克隆的数据更改,不会影响原始数据;

    注意

    1. 必须实现Cloneable接口,否则调用clone方法抛出异常

    Object的clone方法的实现

    protected Object clone() throws CloneNotSupportedException {
       if (!(this instanceof Cloneable)) {
           throw new CloneNotSupportedException("Class " + getClass().getName() +" doesn't implement Cloneable");
       }
       return internalClone();
    }
    
     /*
     * Native helper method for cloning.
     */
    @FastNative
    private native Object internalClone();
    

    2. 深克隆时子类也要复写clone方法

    父类实现了clone方法,如果子类不覆写,那么子类只能浅克隆;

    相关文章

      网友评论

          本文标题:Java 浅克隆、深克隆

          本文链接:https://www.haomeiwen.com/subject/vovotctx.html