7.浅拷贝和深拷贝

一、浅拷贝

拷贝出来的是一个新对象。

对于基本数据类型:会直接进行值传递,会将该属性值复制一份给新的拷贝对象,由于是两份不同的数据,所以如果对原对象的该成员变量值进行改变,不会影响到拷贝后对象的成员变量的值。

对于引用类型:会将引用值(内存地址)进行传递,由于指向堆中同一个对象,所以对原对象的该成员变量值进行改变,会英雄拷贝后对象的成员变量的值。

浅拷贝例子

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 + '\'' +
                '}';
    }
}



输出结果

这是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

二、深拷贝

对于基本数据类型:进行值传递,所以对于原对象和拷贝后的对象来说是两份不同的数据,所以如果对原对象的该成员变量值进行改变,不会影响到拷贝后对象的成员变量的值。

对于引用类型:开辟内存空间进行存储,所以对于原对象和拷贝后的对象来说是两份不同的数据,所以如果对原对象的该成员变量值进行改变,不会影响到拷贝后对象的成员变量的值。

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();
    }
}

输出结果

这是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
posted @ 2019-09-16 21:38  一笑风生  阅读(241)  评论(0编辑  收藏  举报