Java 克隆 Cloneable接口

深克隆:一个类中还有个类对象,如果要对这个类对象也创建一个新的空间,就要进行深克隆。两个类都要实现Cloneable接口,重写clone方法。但是对于Person类clone()方法需要修改。(为所有的对象开辟新的空间)
浅克隆:只为当前对象创建空间,对其属性当中的对象不用开辟新的空间。
Person类:

public class Person implements Cloneable{
    private String name;
    private int age;
    private Address address;

    public Person(String name, int age, Address address) {
        System.out.println("调用了有参构造");
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public Person(String name, int age) {
        System.out.println("调用了有参构造");
        this.name = name;
        this.age = age;
    }

    public Person() {
        System.out.println("调用了无参构造");
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person p=(Person)super.clone();//克隆Person
        p.setAddress((Address)address.clone());//克隆Address
        return p;
    }
}

Address类:

public class Address implements Cloneable{
    private String city;
    private String area;

    public Address() {
        System.out.println("调用了Address的无参构造");
    }

    public Address(String city, String area) {
        System.out.println("调用了Address的有参构造");
        this.city = city;
        this.area = area;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", area='" + area + '\'' +
                '}';
    }

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

测试:

public class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1=new Person("轩轩",38);
        Address address=new Address("中国","上海");
        person1.setAddress(address);
        Person person2=(Person) person1.clone();
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person1.getAddress().hashCode());
        System.out.println(person2.getAddress().hashCode());
    }
}
//答案
调用了有参构造
调用了Address的有参构造
460141958
1163157884
1956725890
356573597

浅克隆:Adress类不需要实现Cloneable接口
Person类:

public class Person implements Cloneable{
    private String name;
    private int age;
    private Address address;

    public Person(String name, int age, Address address) {
        System.out.println("调用了有参构造");
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public Person(String name, int age) {
        System.out.println("调用了有参构造");
        this.name = name;
        this.age = age;
    }

    public Person() {
        System.out.println("调用了无参构造");
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

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

Address类

public class Address{
    private String city;
    private String area;

    public Address() {
        System.out.println("调用了Address的无参构造");
    }

    public Address(String city, String area) {
        System.out.println("调用了Address的有参构造");
        this.city = city;
        this.area = area;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", area='" + area + '\'' +
                '}';
    }
}

测试

public class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1=new Person("轩轩",38);
        Address address=new Address("中国","上海");
        person1.setAddress(address);
        Person person2=(Person) person1.clone();
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person1.getAddress().hashCode());
        System.out.println(person2.getAddress().hashCode());
    }
}
//答案
调用了有参构造
调用了Address的有参构造
460141958
1163157884
1956725890
1956725890

你可能感兴趣的:(java)