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

    浅拷贝:

    class Professor {
        String name;
        int age;
    
        public Professor(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    
    class Student implements Cloneable {
        String name;
        int age;
        Professor p;
    
        public Student(String name, int age, Professor p) {
            this.name = name;
            this.age = age;
            this.p = p;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Student s = (Student) super.clone();
    //        s.p = (Professor) p.clone();
            return s;
        }
    }
    
    public class Main {
        public static void main(String[] args) throws CloneNotSupportedException {
            Professor p = new Professor("教授1", 50);
            Student s1 = new Student("学生1", 18, p);
            Student s2 = (Student) s1.clone();
    
            s2.p.name = "教授2";
            s2.p.age = 30;
            s2.name = "学生2";
            s2.age = 25;
            System.out.println(s1.name + " " + s1.age + " " + s1.p.name + " " + s1.p.age);
        }
    }

    结果: 学生1 18 教授2 30

    深拷贝:

    class Professor implements Cloneable {
        String name;
        int age;
    
        public Professor(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    
    class Student implements Cloneable {
        String name;
        int age;
        Professor p;
    
        public Student(String name, int age, Professor p) {
            this.name = name;
            this.age = age;
            this.p = p;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Student s = (Student) super.clone();
            s.p = (Professor) p.clone();
            return s;
        }
    }
    
    public class Main {
        public static void main(String[] args) throws CloneNotSupportedException {
            Professor p = new Professor("教授1", 50);
            Student s1 = new Student("学生1", 18, p);
            Student s2 = (Student) s1.clone();
    
            s2.p.name = "教授2";
            s2.p.age = 30;
            s2.name = "学生2";
            s2.age = 25;
            System.out.println(s1.name + " " + s1.age + " " + s1.p.name + " " + s1.p.age);
        }
    }

    结果: 学生1 18 教授1 50

    序列化形式深拷贝:

    class Professor implements Serializable {
        private static final long serialVersionUID = 1286716519490813020L;
        String name;
        int age;
    
        public Professor(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
    
    class Student implements Serializable {
        private static final long serialVersionUID = -547004870369127943L;
        String name;
        int age;
        Professor p;
    
        public Student(String name, int age, Professor p) {
            this.name = name;
            this.age = age;
            this.p = p;
        }
    
        public Object deepClone() throws IOException, ClassNotFoundException {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
    
            ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
            return objectInputStream.readObject();
        }
    }
    
    public class Main {
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            Professor p = new Professor("教授1", 50);
            Student s1 = new Student("学生1", 18, p);
            Student s2 = (Student) s1.deepClone();
    
            s2.p.name = "教授2";
            s2.p.age = 30;
            s2.name = "学生2";
            s2.age = 25;
            System.out.println(s1.name + " " + s1.age + " " + s1.p.name + " " + s1.p.age);
        }
    }

    结果: 学生1 18 教授1 50

  • 相关阅读:
    ArcGIS API for javascript开发笔记(四)——GP服务调用之GP模型的规范化制作详解
    ArcGIS API for javascript开发笔记(三)——解决打印输出的中文为乱码问题
    ArcGIS API for javascript开发笔记(二)——解决ArcGIS Service中的服务在内网环境下无法进行javascript预览问题
    解决GP服务产生的结果无法自动发布为地图服务的问题
    解决oracle12c安装报“[INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置)”方法
    Pdf File Writer 中文应用(PDF文件编写器C#类库)
    七牛云存储客户端(本人开发,开源)
    如鹏网 net高级技术 第二章 委托和事件(复习)
    如鹏网 net高级技术 第一章 各种知识点(复习)
    写个QuartzHelper类
  • 原文地址:https://www.cnblogs.com/balfish/p/6306073.html
Copyright © 2011-2022 走看看