zoukankan      html  css  js  c++  java
  • clone 深拷贝 浅拷贝

    1. 定义:知道一个对象,但不知道类,想要得到该对象相同的一个副本,在修改该对象的属性时,副本属性不修改,clone的是对象的属性

    2. 意义:当一个对象里很多属性,想要得到一个相同的对象,还有set很多属性很麻烦

    3. 实现:实现Cloneable接口(标识接口,里面没有方法的定义)标识该类对象可以克隆

                重写Object中的clone()方法,该方法可以合法的(实现上面接口)对该类实例(对象)按字段进行复制。

                在重写的clone()方法中 return super.clone(); (调用父类(Object)的clone方法,其实子类什么都没做)用父类的clone放复制当前对象的属性 返回复制的对象

    4. 深拷贝,浅拷贝:对象属性有 基本类型 int String(不是基本但是final)等,引用变量(其他类的对象),因为基本基本类型变量的拷贝是值拷贝,引用变量的拷贝是拷贝的地址,所以克隆出来的对象的引用变量是同一个变量,在一个对象修改时,另一个对象也被修改,所以有了深拷贝。实现深拷贝需要把引用变量的对象的类也实现Cloneable接口,从写clone()方法,return super.clone(); 

    5. 深拷贝好像用在java框架hibernate 上

    /*
    浅拷贝 s修改teacher s1的teacher也修改了
    */
    package weiguoyuan.chainunicom.cn;
    
    class Teacher1 {
        private String name;
        public Teacher1(String name) {
            this.name = name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            return name;
        }
    }
    
    class Student1 implements Cloneable{
        private String name;
        private Teacher1 teacher;
        public Student1(String name,Teacher1 teacher) {
            this.name = name;
            this.teacher = teacher;
        }
        public Teacher1 getTeacher() {
            return teacher;
        }
        public String toString() {
            return name+" "+teacher.toString();
        }
        public Object clone() throws CloneNotSupportedException{
            return super.clone();
            
        }
    }
    
    public class TestClone {
    
        public static void main(String[] args) throws CloneNotSupportedException {
            Teacher1 t = new Teacher1("jianghongweiSB");
            Student1 s = new Student1("weiguoyuanNB",t);
            Student1 s1 = (Student1)s.clone();
            System.out.println(s);
            System.out.println(s1);
            
            s.getTeacher().setName("jianghongwei");
            System.out.println(s);
            System.out.println(s1);        
        }
    }
    /**
     * 深拷贝实现
     */
    package weiguoyuan.chainunicom.cn;
    
    class Teacher1 implements Cloneable{
        private String name;
        public Teacher1(String name) {
            this.name = name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            return name;
        }
        public Object clone() throws CloneNotSupportedException{
            return super.clone();    
        }
    }
    
    class Student1 implements Cloneable{
        private String name;
        private Teacher1 teacher;
        public Student1(String name,Teacher1 teacher) {
            this.name = name;
            this.teacher = teacher;
        }
        public Teacher1 getTeacher() {
            return teacher;
        }
        public String toString() {
            return name+" "+teacher.toString();
        }
        public Object clone() throws CloneNotSupportedException{
            Student1 student = (Student1)super.clone();
            student.teacher = (Teacher1)this.teacher.clone();
            return student;    
        }
    }
    
    public class TestClone {
    
        public static void main(String[] args) throws CloneNotSupportedException {
            Teacher1 t = new Teacher1("jianghongweiSB");
            Student1 s = new Student1("weiguoyuanNB",t);
            Student1 s1 = (Student1)s.clone();
            System.out.println(s);
            System.out.println(s1);
            
            s.getTeacher().setName("jianghongwei");
            System.out.println(s);
            System.out.println(s1);        
        }
    }

                

  • 相关阅读:
    [转]MongoDB for Java】Java操作MongoDB
    [转]Apache Maven 入门篇 ( 上 )
    [转]Apache Maven 入门篇(下)
    [转]-Dmaven.multiModuleProjectDirectory system propery is not set. 解决方案 适用于myeclipes 和 eclipes
    [转]java代码注释规范
    [转] java编程规范
    [原创]java WEB学习笔记43:jstl 介绍,core库详解:表达式操作,流程控制,迭代操作,url操作
    [转]详解Java解析XML的四种方法
    [原创]java WEB学习笔记42:带标签体的自定义标签,带父标签的自定义标签,el中自定义函数,自定义标签的小结
    [原创]java WEB学习笔记41:简单标签之带属性的自定义标签(输出指定文件,计算并输出两个数的最大值 demo)
  • 原文地址:https://www.cnblogs.com/weixiaole/p/4569548.html
Copyright © 2011-2022 走看看