首页 > 编程语言 >java实现浅拷贝与深拷贝

java实现浅拷贝与深拷贝

时间:2024-07-12 13:27:17浏览次数:13  
标签:city java String People 实现 Address address 拷贝 public

目录

浅拷贝

 深拷贝

实现cloneable接口

序列化


浅拷贝

浅拷贝是指创建一个新的对象,该对象的内容是原始对象中各项的引用。换句话说,浅拷贝仅复制了原始对象中元素的引用,而不是元素本身的拷贝

class  People implements Cloneable{
    private String name;
    private int  age;
    private Address address;
    public People(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address.toString() +
                '}';
    }

    public Address getAddress() {
        return address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Address{
    private String city;

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

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

    public void setCity(String city) {
        this.city = city;
    }
}
public class Clone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address=new Address("杭州");
        People people=new People("李飞",22,address);
        People clonePeople= (People) people.clone();
        System.out.println(people);
        System.out.println(clonePeople);
        //修改 address 值
        address.setCity("北京");
        System.out.println(people);
        System.out.println(clonePeople);
        System.out.println(clonePeople.getAddress().hashCode());
        System.out.println(people.getAddress().hashCode());
    }
}

运行结果:

       原始类 Person 实现 Cloneable 接口,并且覆写 clone 方法,它还有三个属性,一个引用类型 String定义的 name,一个基本类型 int定义的age,还有一个引用类型 Address ,这是一个自定义类,这个类包含 city 属性。

  当我修改了原对象的Address中的city属性(从杭州变更为北京),从输出结果来看,克隆对象的city属性也发生了改变,然后又打印了原对象和克隆对象的Address的哈希地址,发现他们还是同一个对象。这就是浅拷贝,对于引用型对象只会复制引用,他们指向的还是同一块堆内存空间,而不是创建一个新的对象。

 深拷贝

深拷贝是指创建一个新的对象,并递归地复制原始对象及其所有嵌套对象,从而实现完全独立的拷贝。对原对象的操作不会影响新对象。

实现cloneable接口

那如果实现深拷贝呢,第一种方法就是复制对象里的所有引用对象都实现cloneable接口并重写clone 方法就行,这里的自定义的引用对象就是Address,当我们将该对象也实现cloneable接口,就可以实现深拷贝了

package base;

class  People implements Cloneable{
    // People其他属性没展示,可参考上面代码
    @Override
    protected Object clone() throws CloneNotSupportedException {
        People people =(People) super.clone();
        people.address = (Address) address.clone();
        return people;
    }
}
class Address implements Cloneable{
    private String city;

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

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

    public void setCity(String city) {
        this.city = city;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Clone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address=new Address("杭州");
        People people=new People("李飞",22,address);
        People clonePeople= (People) people.clone();
        System.out.println(people);
        System.out.println(clonePeople);
        //修改 address 值
        address.setCity("北京");
        System.out.println(people);
        System.out.println(clonePeople);
        System.out.println(clonePeople.getAddress().hashCode());
        System.out.println(people.getAddress().hashCode());
    }
}

执行结果:

上面主要修改点一个就是Address也实现了 cloneable接口,重写了clone 方法,然后People中clone方法进行了修改,调用了Address的clone方法。

        但是这种做法有个弊端,这里我们Person 类只有一个 Address 引用类型,而 Address 类没有,所以我们只用重写 Address 类的clone 方法,但是如果 Address 类也存在一个引用类型,那么我们也要重写其clone 方法,这样下去,有多少个引用类型,我们就要重写多少次,如果存在很多引用类型,那么代码量显然会很大,所以这种方法不太合适。

序列化

        序列化是将对象写到流中便于传输,而反序列化则是把对象从流中读取出来。这里写到流中的对象则是原始对象的一个拷贝,因为原始对象还存在 JVM 中,所以我们可以利用对象的序列化产生克隆对象,然后通过反序列化获取这个对象。

        注意每个需要序列化的类都要实现 Serializable 接口,如果有某个属性不需要序列化,可以将其声明为 transient,即将其排除在克隆属性之外。

    public Object deepClone() throws Exception{
        // 序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        // 反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ois.readObject();
    }

完整代码:

class  People implements Serializable {
    private String name;
    private int  age;
    private Address address;
    public People(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address.toString() +
                '}';
    }

    public Address getAddress() {
        return address;
    }
    public Object deepClone() throws Exception{
        // 序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        // 反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ois.readObject();
    }
}
class Address implements Serializable{
    private String city;

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

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

    public void setCity(String city) {
        this.city = city;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Clone {

    public static void main(String[] args) throws Exception {
        Address address=new Address("杭州");
        People people=new People("李飞",22,address);
        People clonePeople= (People) people.deepClone();
        System.out.println(people);
        System.out.println(clonePeople);
        //修改 address 值
        address.setCity("北京");
        System.out.println(people);
        System.out.println(clonePeople);
        System.out.println(clonePeople.getAddress().hashCode());
        System.out.println(people.getAddress().hashCode());
    }
}

运行结果:
  

标签:city,java,String,People,实现,Address,address,拷贝,public
From: https://blog.csdn.net/m0_62729850/article/details/140213837

相关文章

  • Day8(字符串)| 151.翻转字符串里的单词 55.右旋转字符串 28.实现 strStr() 459.重复
    151.翻转字符串里的单词给你一个字符串s,请你反转字符串中单词的顺序。单词是由非空格字符组成的字符串。s中使用至少一个空格将字符串中的单词分隔开。返回单词顺序颠倒且单词之间用单个空格连接的结果字符串。注意:输入字符串s中可能会存在前导空格、尾随空格或......
  • python编程实例 计算输入内容中数字、字母、空格、其它字符的数量 两种方式实现
    第一种方式为通过python自带函数实现第二种方式为通过ascii码实现点击查看代码#字符串构成,统计出字符串中#空格英文字符数字其它字符的数量'''使用自带函数a=input("请输入:")kong=0ying=0shu=0qita=0foriinrange(len(a)):if(a[i].isspace()):kong......
  • 【日常记录-Java】自定义进程池
    Author:赵志乾Date:2024-07-12Declaration:AllRightReserved!!!1.简介    服务器上有些进程需要池化管理,使用SpringBoot构建Web服务提供管理api,内部使用自定义的进程池维护已启动的进程;    核心点: 进程池管理的进程都会使用系统的一个端口对外提供服务; ......
  • Redis数据结构—跳跃表 skiplist 实现源码分析
    Redis是一个开源的内存数据结构存储系统,它可以用作数据库、缓存和消息中间件。Redis的数据结构非常丰富,其中跳跃表(skiplist)是一种重要的数据结构,它被用来实现有序集合(sortedsets)。跳跃表是一种概率型数据结构,它通过多层链表来实现快速的查找操作。跳跃表的结构类似于多层索引,......
  • 毕业设计-基于Springboot+Vue的招生管理系统的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456200基于SpringBoot+Vue的招生管理系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1GwKRBbuwMiZmnxkvRN9VJg?pwd=sb......
  • 毕业设计-基于Springboot+Vue的致远汽车租赁系统的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456206基于SpringBoot+Vue的致远汽车租赁系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1IHfaizhpMI1q750DBZ1enA?pw......
  • 毕业设计-基于Springboot+Vue的智慧外贸平台的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456212基于SpringBoot+Vue的智慧外贸平台开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1PnEjpc6IXOgPmYxluTOr2g?pwd=kv......
  • 基于协同过滤混合算法的餐饮推荐系统设计与实现
    基于协同过滤混合算法的餐饮推荐系统设计与实现DesignandImplementationofRestaurantRecommendationSystemBasedonHybridCollaborativeFilteringAlgorithm完整下载链接:基于协同过滤混合算法的餐饮推荐系统设计与实现文章目录基于协同过滤混合算法的餐饮......
  • Java怎么统计每个项目下的每个类别的数据
    为了演示如何在Java中统计每个项目下的每个类别的数据,我们可以考虑一个简单的场景:假设我们有一个电商系统,需要统计每个商品分类在每个店铺下的销售数量。这里我们将使用Java的集合框架,如HashMap和ArrayList,来存储和统计数据。1.使用Java的集合框架HashMap和ArrayList来存储和统计......
  • 关于Java内存区域的理解和记录
    近期做项目遇到了FullGC的问题,干脆总结一下Java内存区域分布和垃圾回收是咋回事。Java内存区域按照线程隔离状态直接分成三大块空间:线程私有:程序计数器是一块较小的内存空间,可以看做当前线程所执行的字节码的行号指示器。在虚拟机概念模型里,字节码解释器工作时就是通过改变这......