Java教程

谈谈java的浅复制和深复制

本文主要是介绍谈谈java的浅复制和深复制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.浅复制和深复制的概念
浅复制是指当复制某个对象时,仅仅复制该对象的基本数据类型,而不复制其引用数据类型;而深复制不仅复制其基本数据类型,而且复制其引用数据类型。无论浅复制还是深复制,在使用该功能之前对象都实现Cloneable接口,重写clone()方法。特别注意,复制对象和原对象不是同一个对象,但复制对象和原对象的类型是一样的。

2.实例代码

2.1 浅复制代码

Person类

​
public class Person implements Cloneable {
    // 姓名
    private String name;
    // 年龄
    private String age;
    // 汽车
    private Car car;    

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    
    public Car getCar() {
        return car;
    }
    
    public void setCar(Car car) {
        this.car = car;
    }
    
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
}​

Car类

​
public class Car {
    // 车名
    private String name;
    // 颜色
    private String color;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public String toString() {
        return "Car [name=" + name + ", color=" + color + "]";
    }
    
}
​

测试类

​
public class ShadowCopyTest {
    public static void main(String[] args) {
        
        Person p1 = new Person();
        p1.setName("Tom");
        p1.setAge("25");
        
        Car c1 = new Car();
        c1.setName("BMW");
        c1.setColor("Brown");
        p1.setCar(c1);
        
        try {
            Person p2 = (Person) p1.clone();
            Car c2 = p2.getCar();
            c2.setName("BYD");
            c2.setColor("Black");
            p2.setCar(c2);
            System.out.println("p1: " + p1);
            System.out.println("p2: " + p2);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

    }
}

​

测试结果

​2.2 深复制代码

Person类

​
public class Person implements Cloneable {
    // 姓名
    private String name;
    // 年龄
    private String age;
    // 汽车
    private Car car;
    
    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    
    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
    }

    public Object clone() throws CloneNotSupportedException {
        Person newPerson = (Person) super.clone();
        newPerson.car = (Car) car.clone();
        return newPerson;
    }
    
}

​

Car类

​
public class Car implements Cloneable {
    // 车名
    private String name;
    // 颜色
    private String color;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public String toString() {
        return "Car [name=" + name + ", color=" + color + "]";
    }
    
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
}

​

测试类

public class DeepCopyTest {
	public static void main(String[] args) {
		
		Person p1 = new Person();
		p1.setName("Tom");
		p1.setAge("25");
		
		Car c1 = new Car();
		c1.setName("BMW");
		c1.setColor("Brown");
		p1.setCar(c1);
		
		try {
			Person p2 = (Person) p1.clone();
			Car c2 = p2.getCar();
			c2.setName("BYD");
			c2.setColor("Black");
			p2.setCar(c2);
			System.out.println("p1: " + p1);
			System.out.println("p2: " + p2);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}

	}
}
测试结果

 

这篇关于谈谈java的浅复制和深复制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!