深拷贝和浅拷贝

本文通过Java代码实例详细解释了深拷贝和浅拷贝的区别。在浅拷贝中,拷贝的对象与原对象共享同一块内存空间,修改源对象会影响到拷贝对象。而在深拷贝中,拷贝的对象是完全独立的,修改源对象不会影响到拷贝对象。示例展示了当对象中包含引用时,如何实现深拷贝以确保拷贝对象的独立性。
摘要由CSDN通过智能技术生成

一、深拷贝和浅拷贝

1.深拷贝

        深拷贝是指源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。

        也就是说,深拷贝把要复制的对象所引用的对象都复制了一遍。

2.浅拷贝

        拷贝出来的目标对象的指针和源对象的指针指向的内存空间是同一块空间,浅拷贝只是一种简单的拷贝。

        也就是说,浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象。

3.深拷贝和浅拷贝都是对象拷贝

二、具体实列

1.浅拷贝

public class Copy {
	    public static void main(String[] args) throws CloneNotSupportedException {
	        Master master = new Master();
	        master.setName("A");
	        master.setAge(20);

	        Dog dog1 = new Dog();
	        dog1.setName("DOG");
	        dog1.setAge(4);
	        dog1.setMaster(master);
	        Dog dog2 = (Dog) dog1.clone();
	        System.out.println("-------------拷贝后-------------");
	        System.out.println(dog2.getName());
	        System.out.println(dog2.getAge());
	        System.out.println("master:");
	        System.out.println(dog2.getMaster().getName());
	        System.out.println(dog2.getMaster().getAge());

	        System.out.println("-------------修改master的信息后-------------");
	        master.setName("B");
	        System.out.println("Dog1的master为: " + dog1.getMaster().getName());
	        System.out.println("Dog2的master为: " + dog2.getMaster().getName());

	    }
	}

	class Master implements Cloneable{
	    private String name;
	    private int age;

	    public String getName() {
	        return name;
	    }
	    public void setName(String name) {
	        this.name = name;
	    }
	    public int getAge() {
	        return age;
	    }
	    public void setAge(int age) {
	        this.age = age;
	    }
	}

	class Dog implements Cloneable {
	    private String name;
	    private int age;
	    private Master master;

	    public String getName() {
	        return name;
	    }
	    public void setName(String name) {
	        this.name = name;
	    }
	    public int getAge() {
	        return age;
	    }
	    public void setAge(int age) {
	        this.age = age;
	    }
	    public Master getMaster() {
	        return master;
	    }
	    public void setMaster(Master master) {
	        this.master = master;
	    }
	    public Object clone() throws CloneNotSupportedException {
	        Object object = super.clone();
	        return object;
	    }
	}

运行结果:

-------------拷贝后-------------
DOG
4
master:
A
20
-------------修改master的信息后-------------
Dog1的master为: B
Dog2的master为: B

2.深拷贝

public class Copy {
	    public static void main(String[] args) throws CloneNotSupportedException {
	        Master master = new Master();
	        master.setName("A");
	        master.setAge(28);

	        Dog dog1 = new Dog();
	        dog1.setName("DOG");
	        dog1.setAge(4);
	        dog1.setMaster(master);
	        Dog dog2 = (Dog) dog1.clone();
	        System.out.println("-------------拷贝后-------------");
	        System.out.println(dog2.getName());
	        System.out.println(dog2.getAge());
	        System.out.println("master:");
	        System.out.println(dog2.getMaster().getName());
	        System.out.println(dog2.getMaster().getAge());

	        System.out.println("-------------修改master的信息后-------------");
	        master.setName("B");
	        System.out.println("dog1的master为: " + dog1.getMaster().getName());
	        System.out.println("dog2的master为: " + dog2.getMaster().getName());
	    }
	}

	class Master implements Cloneable {
	    private String name;
	    private int age;

	    public String getName() {
	        return name;
	    }
	    public void setName(String name) {
	        this.name = name;
	    }
	    public int getAge() {
	        return age;
	    }
	    public void setAge(int age) {
	        this.age = age;
	    }
	    public Object clone() throws CloneNotSupportedException {
	        return super.clone();
	    }
	}

	class Dog implements Cloneable {
	    private String name;
	    private int age;
	    private Master master;

	    public String getName() {
	        return name;
	    }
	    public void setName(String name) {
	        this.name = name;
	    }
	    public int getAge() {
	        return age;
	    }
	    public void setAge(int age) {
	        this.age = age;
	    }
	    public Master getMaster() {
	        return master;
	    }
	    public void setMaster(Master master) {
	        this.master = master;
	    }
	    public Object clone() throws CloneNotSupportedException {
	        Dog dog = (Dog) super.clone();
	        dog.setMaster((Master) dog.getMaster().clone());
	        return dog;
	    }
	}

运行结果:

-------------拷贝后-------------
DOG
4
master:
A
28
-------------修改master的信息后-------------
dog1的master为: B
dog2的master为: A

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值