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

  • 相关阅读:
    Appium脚本(2):元素检测
    查看appPackage和appActivity的多种方法
    让织梦内容页arclist标签的当前文章标题加亮显示
    dedecms wap 上一篇 下一篇 链接出错
    织梦开启二级域名(多站点)内容页图片无法显示的解决方法
    多级分类标签{dede:channelartlist}实现当前栏目颜色高亮显示
    织梦channelartlist标签当前栏目高亮
    dedecms模板中 if else怎么写
    dedecms调用子栏目及文章列表
    Dedecms判断当前栏目下是否有子栏目
  • 原文地址:https://www.cnblogs.com/balfish/p/6306073.html
Copyright © 2011-2022 走看看