深克隆和浅克隆

浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。

深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。

总之深浅克隆都会在堆中新分配一块区域,区别在于对象属性引用的对象是否需要进行克隆(递归性的)。

请看以下代码:

public class Address {
    private String shenghui;
    private String cheengshi;

    public String getShenghui() {
        return shenghui;
    }

    public void setShenghui( String shenghui ) {
        this.shenghui = shenghui;
    }

    public String getCheengshi() {
        return cheengshi;
    }

    public void setCheengshi( String cheengshi ) {
        this.cheengshi = cheengshi;
    }

    public  Address( String shenghui, String cheengshi){
        this.cheengshi=cheengshi;
        this.shenghui=shenghui;
    }
    @Override
    public String toString() {
        return "Address{" +
                "shenghui='" + shenghui + '\'' +
                ", cheengshi='" + cheengshi + '\'' +
                '}';
    }
   
}

 

public class Father implements Cloneable {
    private int age;
    private String name;
    private Address add;

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public Address getAdd() {
        return add;
    }

    public void setAge( int age ) {
        this.age = age;
    }

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

    @Override
    public String toString() {
        return "Father{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", add=" + add +
                '}';
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        Father fa= (Father) super.clone();
        fa.add= (Address) add.clone();
        return  fa;
    }

    public static void main(String args[]) throws CloneNotSupportedException {
        Father fa=new Father();
        Address ad=new Address("湖北省","武汉市");
        fa.setAdd(ad);
        fa.setAge(18);
        fa.setName("aaa");
        String name=fa.getName();

        Father copyFather= (Father) fa.clone();
        System.out.println(fa==copyFather);//这里输出false说明两个对象个开辟了一个不同的内存空间
        System.out.println(fa.toString());
        System.out.println(copyFather.toString());

        copyFather.setAge(19);//对于基本类型,一个改变不会影响另一个
        fa.setName("bbb");
        System.out.println(fa.toString());
        System.out.println(copyFather.toString());

        ad.setCheengshi("杭州市");//这边把fa的城市改变,克隆的对象的城市也跟着改变了,说明原来的对象和克隆对象里面的属
//性指向同一个属性
        System.out.println(fa.toString());
        System.out.println(copyFather.toString());
    }

}
对于输出结果:

深克隆和浅克隆_第1张图片

那么如何实现深克隆的,浅克隆到深克隆,只需要把类里面每个非基本类型属性都克隆一下

此时:

public class Address implements Cloneable {
    private String shenghui;
    private String cheengshi;

    public String getShenghui() {
        return shenghui;
    }

    public void setShenghui( String shenghui ) {
        this.shenghui = shenghui;
    }

    public String getCheengshi() {
        return cheengshi;
    }

    public void setCheengshi( String cheengshi ) {
        this.cheengshi = cheengshi;
    }

    public  Address( String shenghui, String cheengshi){
        this.cheengshi=cheengshi;
        this.shenghui=shenghui;
    }
    @Override
    public String toString() {
        return "Address{" +
                "shenghui='" + shenghui + '\'' +
                ", cheengshi='" + cheengshi + '\'' +
                '}';
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }


}

 

/**
 * Created by 婷灵 on 2019/9/14.
 */
public class Father implements Cloneable {
    private int age;
    private String name;
    private Address add;

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public Address getAdd() {
        return add;
    }

    public void setAge( int age ) {
        this.age = age;
    }

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

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

    public void setAdd( Address add ) {
        this.add = add;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        Father fa= (Father) super.clone();
        fa.add= (Address) add.clone();
        return  fa;
    }

    public static void main(String args[]) throws CloneNotSupportedException {
        Father fa=new Father();
        Address ad=new Address("湖北省","武汉市");
        fa.setAdd(ad);
        fa.setAge(18);
        fa.setName("aaa");
        String name=fa.getName();

        Father copyFather= (Father) fa.clone();
        System.out.println(fa==copyFather);
        System.out.println(fa.toString());
        System.out.println(copyFather.toString());

        copyFather.setAge(19);
        fa.setName("bbb");
        System.out.println(fa.toString());
        System.out.println(copyFather.toString());

        ad.setCheengshi("杭州市");
        System.out.println(fa.toString());
        System.out.println(copyFather.toString());
    }

}
看此时的输出

深克隆和浅克隆_第2张图片

这时候我们改变本对象属性值没有对克隆对象的属性值起到作用

你可能感兴趣的:(面试题)