Java的深克隆的浅克隆

763 阅读2分钟

Java的深克隆的浅克隆

1、浅拷贝和深拷贝

  • 浅拷贝:复制的是目标对象,目标对象的引用并不会复制,还是原来的引用
  • 深拷贝:复制的是目标对象以及目标对象中的引用对象。

2、clone方法

  • 在Java的Object对象中,有clone这个方法。它被声明为了 protected,所以我们可以在其子类中使用它。这里需要注意的是,我们在子类中使用clone方法时,子类需要实现Cloneable接口,否则会抛出java.lang.CloneNotSupportedException异常。

3、浅拷贝实现代码

@Data
public class Person implements Cloneable{

    private String name;

    private Integer age;

    private Address address;

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

@Data
public class Address {

    private String name;
}


public class Client {

    public static void main(String[] args) throws      				CloneNotSupportedException {
        Address address = new Address();
        address.setName("浙江省杭州市");
        Person person = new Person();
        person.setAge(23);
        person.setName("joker");
        person.setAddress(address);
        Person copy = (Person) person.clone();
        System.out.println(copy == person);			     System.out.println(copy.getAddress()==person.getAddress());
        address.setName("宁波");
        System.out.println(person.getAddress().toString());
        System.out.println(copy.getAddress().toString());
    }
}

结果:
false
true
Address(name=宁波)
Address(name=宁波)

4、浅拷贝存在的问题

  • 当拷贝的目标对象中,存在别的引用对象,例如Person中存在Address的引用。如果修改address的属性的话,拷贝兑现的address的属性也会被改变

5、深拷贝的实现

  • clone

    @Data
    public class Address implements Cloneable{
    
        private String name;
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    
    public static void main(String[] args) throws CloneNotSupportedException {
            Address address = new Address();
            address.setName("浙江省杭州市");
            Person person = new Person();
            person.setAge(23);
            person.setName("joker");
            person.setAddress(address);
            Person copy = (Person) person.clone();
            copy.setAddress((Address) person.getAddress().clone());
            System.out.println(copy == person);
            System.out.println(copy.getAddress()==person.getAddress());
            address.setName("宁波");
            System.out.println(person.getAddress().toString());
            System.out.println(copy.getAddress().toString());
        }
    
    结果:
    false
    true
    Address(name=宁波)
    Address(name=浙江省杭州市)
    
  • 序列化

    public class Client {
    
        public static void main(String[] args) throws CloneNotSupportedException {
            Address address = new Address();
            address.setName("浙江省杭州市");
            Person person = new Person();
            person.setAge(23);
            person.setName("joker");
            person.setAddress(address);
            byte[] serial = SerializationUtils.serialize(person);
            Person copy = (Person) SerializationUtils.deserialize(serial);
            System.out.println(copy == person);
            System.out.println(copy.getAddress()==person.getAddress());
            address.setName("宁波");
            System.out.println(person.getAddress().toString());
            System.out.println(copy.getAddress().toString());
        }
    }
    

转载说明:

本文发布于掘金号【Happyjava】。Happy的掘金地址:juejin.im/user/5cc289…,Happy的个人博客:blog.happyjava.cn。欢迎转载,但须保留此段声明。