7. shallow vs. deep copy

A shallow copy

A copy out of the new object.

For the basic data types: directly a value passed will copy the attribute value to a new copy of the object, since the two different data, so if the original object member variable changes value, does not affect copies of the value of the object's member variables.

For reference types: it will reference value (memory address) can be passed, since the same object point to the heap, so that the original object member variable changes value, the value after the copy of the hero object member variables.

Examples shallow copy

package javaBasic_8;

import lombok.Data;

/**
 * @ClassName: CloneTest
 * @Description: 用来实现浅clone
 * @Autohr: 落魄山陈十一
 * @Date: 2019/9/16 20:13
 * @Version: 1.0
 **/
public class BasicClone {
    public static void main(String[] args) {
        City china = new City();
        china.setCityName("china");
        World earth = new World();
        earth.setCityOfWorld(china);
        earth.setWorldName("earth");
        earth.setPower(1);
        World water = null;
        try {
            water = earth.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println("这是earth:" + earth.toString());
        System.out.println("earth的hashCode: " + earth.hashCode());
        System.out.println("earth中name(String对象)的hash值:" + earth.getWorldName().hashCode());
        System.out.println("earth中city对象的hash值:" + earth.getCityOfWorld().hashCode());
        System.out.println("这是water: " + water.toString());
        System.out.println("water的hashCode: " + water.hashCode());
        System.out.println("water中name(String对象)的hash值: " + earth.getWorldName().hashCode());
        System.out.println("water中city对象的hash值:" + water.getCityOfWorld().hashCode());

    }

}


class World implements Cloneable{
    private int power;
    private String worldName;
    private City cityOfWorld;

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

    public String getWorldName() {
        return worldName;
    }

    public void setWorldName(String worldName) {
        this.worldName = worldName;
    }

    public City getCityOfWorld() {
        return cityOfWorld;
    }

    public void setCityOfWorld(City cityOfWorld) {
        this.cityOfWorld = cityOfWorld;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    @Override
    public String toString() {
        return "World{" +
                "power=" + power +
                ", worldName='" + worldName + '\'' +
                ", cityOfWorld=" + cityOfWorld +
                '}';
    }
}


class City implements Cloneable{
    private String cityName;

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    @Override
    public String toString() {
        return "City{" +
                "cityName='" + cityName + '\'' +
                '}';
    }
}


Output

这是earth:World{power=1, worldName='earth', cityOfWorld=City{cityName='china'}}
earth的hashCode: 1956725890
earth中name(String对象)的hash值:96278602
earth中city对象的hash值:356573597
这是water: World{power=1, worldName='earth', cityOfWorld=City{cityName='china'}}
water的hashCode: 1735600054
water中name(String对象)的hash值: 96278602
water中city对象的hash值:356573597

Second, the deep copy

For the basic data types: a value transfer, so that after the object is the original object and copy two different data, so if the original object member variable changes value, does not affect the object member variable copy value.

For reference types: open up memory space for storage, so the object after the original object and the copy is two different data, so if you make changes to the variable value of the original members of the object, the object does not affect members after copy value of the variable.

public class DeepClone {
    public static void main(String[] args) {
        Engine bc = new Engine();
        bc.setEngigneName("beci");
        Car qq = new Car();
        qq.setEngineOfCar(bc);
        qq.setCarName("qq");
        Car bmw = null;
        try {
            bmw = qq.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        System.out.println("这是qq:" + qq.toString());
        System.out.println("qq的hashCode: " + qq.hashCode());
        System.out.println("qq中name(String对象)的hash值:" + qq.getEngineOfCar().hashCode());
        System.out.println("qq中engine对象的hash值:" + qq.getEngineOfCar().hashCode());
        System.out.println("这是bmw: " + bmw.toString());
        System.out.println("bmw的hashCode: " + bmw.hashCode());
        System.out.println("bmw中name(String对象)的hash值: " + bmw.getEngineOfCar().hashCode());
        System.out.println("bmw中engine对象的hash值:" + bmw.getEngineOfCar().hashCode());

    }
}

class Car implements Cloneable{
    private String carName;
    private Engine engineOfCar;

    @Override
    protected Car clone() throws CloneNotSupportedException {
        Car car = (Car) super.clone();
        car.setEngineOfCar(car.getEngineOfCar().clone());
        return car;
    }

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public Engine getEngineOfCar() {
        return engineOfCar;
    }

    public void setEngineOfCar(Engine engineOfCar) {
        this.engineOfCar = engineOfCar;
    }

    @Override
    public String toString() {
        return "Car{" +
                "carName='" + carName + '\'' +
                ", engineOfCar=" + engineOfCar +
                '}';
    }
}


class Engine  implements Cloneable{
    private String engigneName;

    public String getEngigneName() {
        return engigneName;
    }

    public void setEngigneName(String engigneName) {
        this.engigneName = engigneName;
    }

    @Override
    public String toString() {
        return "Engine{" +
                "engigneName='" + engigneName + '\'' +
                '}';
    }

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

Output

这是qq:Car{carName='qq', engineOfCar=Engine{engigneName='beci'}}
qq的hashCode: 1956725890
qq中name(String对象)的hash值:356573597
qq中city对象的hash值:356573597
这是bmw: Car{carName='qq', engineOfCar=Engine{engigneName='beci'}}
bmw的hashCode: 1735600054
bmw中name(String对象)的hash值: 21685669
bmw中city对象的hash值:21685669

Guess you like

Origin www.cnblogs.com/dearcabbage/p/11530370.html