加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

2种方法实现java对象的深拷贝

发布时间:2020-12-14 06:39:17 所属栏目:Java 来源:网络整理
导读:pre class="java"span style="font-size:14px;"/** 深拷贝和浅拷贝的测试 */ //测试类1 class Person implements Cloneable{ String name; int age; Person(String name,int age){ this.name=name; this.age=age; } @Override public Object clone() { try{







<pre class="java"><span style="font-size:14px;">/**

  • 深拷贝和浅拷贝的测试
    */
    //测试类1
    class Person implements Cloneable{
    String name;
    int age;
    Person(String name,int age){
    this.name=name;
    this.age=age;
    }
    @Override
    public Object clone() {
    try{
    return super.clone();
    }catch(CloneNotSupportedException e){
    return null;
    }
    }
    }
    //测试类2

class Animal implements Cloneable{
Person host;//主人
int age;//年纪
Animal(Person person,int age){
this.host=person;
this.age=age;
}
@Override
public Object clone(){
try{
Animal animal=(Animal) super.clone();
animal.host=(Person)host.clone();//深拷贝处理
return animal;
}catch (CloneNotSupportedException e){
return null;
}
}
}

//测试
public class Main{
public static void main(String[] args) {
Person person1=new Person("cxh",26);
Person person2=(Person)person1.clone();
System.out.println("----------------浅拷贝--------------");
//测试Object的clone方法为浅拷贝
//String类用==测试内存地址是否一致
System.out.println("person1和person2的name内存地址是否相同:"+(person1.name==person2.name));

            System.out.println("----------------深拷贝--------------");
            //重写Object的clone方法,实现深拷贝
            //还是用==查看两个对象的内存地址是否相等来确定是否为两个对象,如果是两个内存地址,那么就是深拷贝
            Animal animal1=new Animal(new Person("cxh",26),3);
            Animal animal2=(Animal) animal1.clone();
            System.out.println("animal1和animal2的host内存地址是否相同:"+(animal1.host==animal2.host));
    }

}
<span style="font-size:14px;">
输出:


Process finished with exit code 0

<span style="font-size:14px;">



/**

  • 深拷贝和浅拷贝的测试

  • 如何利用序列化来完成对象的拷贝呢?在内存中通过字节流的拷贝是比较容易实现的。把母对象写入到一个字节流中,再从字节流中将其读出来,

  • 这样就可以创建一个新的对象了,并且该新对象与母对象之间并不存在引用共享的问题,真正实现对象的深拷贝。
    */
    //工具类
    class CloneUtil{
    public static T clone(T obj){
    T cloneObj=null;
    try{
    //写入字节流
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    ObjectOutputStream oos=new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.close();

                     //分配内存,写入原始对象,生成新对象
                     ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());//获取上面的输出字节流
                     ObjectInputStream ois=new ObjectInputStream(bais);
    
                     //返回生成的新对象
                     cloneObj=(T)ois.readObject();
                     ois.close();
             }catch (Exception e){
                    e.printStackTrace();
             }
             return cloneObj;
     }

    }

//测试类1
class Person implements Serializable{
String name;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
}

}
//测试类2

class Animal implements Serializable{
Person host;//主人
int age;//年纪
Animal(Person person,int age){
this.host=person;
this.age=age;
}
}

//测试
public class Main{
public static void main(String[] args) {
System.out.println("----------------深拷贝--------------");
//重写Object的clone方法,3);
Animal animal2=CloneUtil.clone(animal1);
System.out.println("animal1和animal2的host内存地址是否相同:"+(animal1.host==animal2.host));
}
}

输出结果:

参考博客:<a href="http://blog.csdn.net/chenssy/article/details/12952063" rel="nofollow">http://blog.csdn.net/chenssy/article/details/12952063

                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读