Java中象拷贝的两种方式:深拷贝与浅拷贝

在Java中,深拷贝和浅拷贝是对象拷贝的两种方式,它们的主要区别如下:

一、区别

  1. 浅拷贝
    • 浅拷贝只复制对象的基本数据类型成员变量的值,以及对象的引用变量的地址值,而不复制引用指向的对象内容。
    • 也就是说,如果原对象中的引用类型变量指向的对象发生变化,浅拷贝得到的对象中的相应引用变量也会反映这个变化。
  2. 深拷贝
    • 深拷贝不仅复制对象的基本数据类型成员变量的值,还会为引用类型的成员变量创建新的对象,并复制其内容。
    • 这样,原对象和深拷贝得到的对象完全独立,修改其中一个对象的任何成员变量都不会影响另一个对象。

二、实现方式及代码示例

  1. 浅拷贝实现方式
    • 可以通过实现 Cloneable 接口并重写 Object 类的 clone () 方法来实现浅拷贝。
class Person implements Cloneable {
       private int age;
       private String name;

       public Person(int age, String name) {
           this.age = age;
           this.name = name;
       }

       public int getAge() {
           return age;
       }

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

       public String getName() {
           return name;
       }

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

       @Override
       protected Object clone() throws CloneNotSupportedException {
           return super.clone();
       }
   }
public class ShallowCopyExample {
       public static void main(String[] args) {
           Person person1 = new Person(30, "Alice");
           try {
               Person person2 = (Person) person1.clone();
               System.out.println("Person1: " + person1.getName() + ", " + person1.getAge());
               System.out.println("Person2: " + person2.getName() + ", " + person2.getAge());
           } catch (CloneNotSupportedException e) {
               e.printStackTrace();
           }
       }
   }
  1. 深拷贝实现方式
    • 同样实现 Cloneable 接口并重写 clone () 方法,但在 clone () 方法中对引用类型的成员变量也进行深度复制。
class Address implements Cloneable {
       private String city;

       public Address(String city) {
           this.city = city;
       }

       public String getCity() {
           return city;
       }

       public void setCity(String city) {
           this.city = city;
       }

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

   class PersonDeepCopy implements Cloneable {
       private int age;
       private String name;
       private Address address;

       public PersonDeepCopy(int age, String name, Address address) {
           this.age = age;
           this.name = name;
           this.address = address;
       }

       public int getAge() {
           return age;
       }

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

       public String getName() {
           return name;
       }

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

       public Address getAddress() {
           return address;
       }

       public void setAddress(Address address) {
           this.address = address;
       }

       @Override
       protected Object clone() throws CloneNotSupportedException {
           PersonDeepCopy clonedPerson = (PersonDeepCopy) super.clone();
           clonedPerson.address = (Address) address.clone();
           return clonedPerson;
       }
   }
public class DeepCopyExample {
       public static void main(String[] args) {
           Address address = new Address("New York");
           PersonDeepCopy person1 = new PersonDeepCopy(30, "Alice", address);
           try {
               PersonDeepCopy person2 = (PersonDeepCopy) person1.clone();
               System.out.println("Person1: " + person1.getName() + ", " + person1.getAge() + ", " + person1.getAddress().getCity());
               System.out.println("Person2: " + person2.getName() + ", " + person2.getAge() + ", " + person2.getAddress().getCity());
               person1.getAddress().setCity("Los Angeles");
               System.out.println("After changing person1's address city");
               System.out.println("Person1: " + person1.getName() + ", " + person1.getAge() + ", " + person1.getAddress().getCity());
               System.out.println("Person2: " + person2.getName() + ", " + person2.getAge() + ", " + person2.getAddress().getCity());
           } catch (CloneNotSupportedException e) {
               e.printStackTrace();
           }
       }
   }

另外,也可以通过序列化的方式实现深拷贝,但这种方式相对复杂一些。深拷贝和浅拷贝在不同的场景下有不同的用途,需要根据具体的需求来选择合适的拷贝方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@程序员小袁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值