博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java深copy (伪深copy)【原】
阅读量:4510 次
发布时间:2019-06-08

本文共 3462 字,大约阅读时间需要 11 分钟。

 

Teacher.java

package test.clone;/** * 老师 * 深copy需要实现Cloneable接口 * @author King * */public class Teacher 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;    }    @Override    public String toString() {        return "Teacher [name=" + name + ", age=" + age + "]";    }    public Object clone() throws CloneNotSupportedException {        return super.clone();    }}

 

Student.java

package test.clone;/** * 学生 * 浅copy需要实现Cloneable接口 * @author King * */public class Student implements Cloneable {    private String name;    private int age;    private Teacher teacher;    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 Teacher getTeacher() {        return teacher;    }    public void setTeacher(Teacher teacher) {        this.teacher = teacher;    }    @Override    public String toString() {        return "Student [name=" + name + ", age=" + age + ", Teacher=" + teacher + "]";    }    //浅copy    public Object clone() throws CloneNotSupportedException {        return super.clone();    }        /**     * 浅copy (同clone()方法)     * 浅copy只对基本类型和String类型做全新复制,     * 属性对象引用是不会全新复制的,最终新copy出来的属性对象引用的还是同一个堆内存区域,比如teacher属性     * @return     * @throws CloneNotSupportedException     * ......     * @time 2018年1月26日 下午8:07:11     * @author King     */    public Object shallowClone() throws CloneNotSupportedException {        return super.clone();    }        /**     * 伪深copy     * 这种伪深copy模式,其实还是使用了浅copy技术,只是把属性对象再次赋了新的浅copy.     * 当对象比较简单时可以用这种模式,因为它比序列化深copy要来得快,还是定制化copy哪些属性     * @return     * @throws CloneNotSupportedException     * ......     * @time 2018年1月26日 下午8:09:39     * @author King     */    public Object deepClone() throws CloneNotSupportedException {        Student stu = (Student) super.clone();        Teacher t2 =  (Teacher) teacher.clone();        stu.setTeacher(t2);        return stu;    }}

 

 

FakeDeepCopy.java 

伪深copy调用样例

package test.clone;/** * fakeDeepCopy,其实是一种伪深copy,对象对比简单时可以使用这种技术  *  * @author King * */public class FakeDeepCopy {    public static void main(String[] args) {        Teacher techarAAA = new Teacher();        techarAAA.setName("Teacher AAA");        techarAAA.setAge(30);        Student studentAAA = new Student();        studentAAA.setName(new String("Student AAA"));        studentAAA.setAge(15);        studentAAA.setTeacher(techarAAA);        System.out.println("学生复制前studentAAA:" + studentAAA);        try {            Student studentCopy = (Student) studentAAA.clone();            Teacher teacherCopy = studentCopy.getTeacher();            studentCopy.setName(new String("Student BBB"));            studentCopy.setAge(20);            teacherCopy.setName("Teacher BBB");            teacherCopy.setAge(45);            studentCopy.setTeacher(teacherCopy);            System.out.println("学生复制后studentAAA:" + studentAAA);            System.out.println("学生复制后studentCopy:" + studentCopy);        } catch (CloneNotSupportedException e) {            e.printStackTrace();        }    }}

以上深copy主要通过各层浅copy实现.

真正完整深copy可通过序列化的方式.

转载于:https://www.cnblogs.com/whatlonelytear/p/8361723.html

你可能感兴趣的文章
Linux下vim上编辑实现进度条
查看>>
ubuntu 设置plank开机自启之后关机键失效变为注销键
查看>>
JS--我发现,原来你是这样的JS(三)(基础概念--灵魂篇)
查看>>
手指滑动切换手机图片
查看>>
解决Oracle EM无法启动
查看>>
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
查看>>
PHP 跨域资源共享 CORS 设定
查看>>
男神鹏:使用Redis 的一些 问题解决方案。
查看>>
创建空间参考
查看>>
TestFlight下载app 初使用
查看>>
promise学习
查看>>
在vagrant官网下载各种最新.box资源
查看>>
selenium+python自动化95-弹出框死活定位不到
查看>>
关于防止用户表单多次提交方案的思考
查看>>
MAC终端显示tree命令
查看>>
Dissecting the First C# Program "HelloWorld"
查看>>
多线程--生产者消费者--简单例子
查看>>
Mac 安装tensorflow
查看>>
jsoup html解析器 实现对博客园博文标题链接抓取
查看>>
数据库面试题
查看>>