关于java:面试官Java深拷贝和浅拷贝区别

30次阅读

共计 4205 个字符,预计需要花费 11 分钟才能阅读完成。

一、拷贝的引入

(1)、援用拷贝

创立一个指向对象的援用变量的拷贝。

Teacher teacher = new Teacher("Taylor",26);
Teacher otherteacher = teacher;
System.out.println(teacher);
System.out.println(otherteacher);

输入后果:

blog.Teacher@355da254
blog.Teacher@355da254

后果剖析:由输入后果能够看出,它们的地址值是雷同的,那么它们必定是同一个对象。teacher 和 otherteacher 的只是援用而已,他们都指向了一个雷同的对象 Teacher(“Taylor”,26)。这就叫做援用拷贝。

(2)、对象拷贝

创建对象自身的一个正本。

Teacher teacher = new Teacher("Swift",26); 
Teacher otherteacher = (Teacher)teacher.clone(); 
System.out.println(teacher);
System.out.println(otherteacher);

输入后果:

blog.Teacher@355da254
blog.Teacher@4dc63996

后果剖析:由输入后果能够看出,它们的地址是不同的,也就是说创立了新的对象,而不是把原对象的地址赋给了一个新的援用变量, 这就叫做对象拷贝。

注:深拷贝和浅拷贝都是对象拷贝

二、浅拷贝

(1)、定义

被复制对象的所有变量都含有与原来的对象雷同的值,而所有的对其余对象的援用依然指向原来的对象。即对象的浅拷贝会对“主”对象进行拷贝,但不会复制主对象外面的对象。”外面的对象“会在原来的对象和它的正本之间共享。

简而言之,浅拷贝仅仅复制所思考的对象,而不复制它所援用的对象

(2)、浅拷贝实例

package com.test;

public class ShallowCopy {public static void main(String[] args) throws CloneNotSupportedException {Teacher teacher = new Teacher();
        teacher.setName("riemann");
        teacher.setAge(27);

        Student2 student1 = new Student2();
        student1.setName("edgar");
        student1.setAge(18);
        student1.setTeacher(teacher);

        Student2 student2 = (Student2) student1.clone();
        System.out.println("拷贝后");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());

        System.out.println("批改老师的信息后 -------------");
        // 批改老师的信息
        teacher.setName("Games");
        System.out.println(student1.getTeacher().getName());
        System.out.println(student2.getTeacher().getName());

    }
}

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;}
}

class Student2 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;}

    public Object clone() throws CloneNotSupportedException {Object object = super.clone();
        return object;
    }
}

输入后果:

 拷贝后
edgar
18
riemann
27
批改老师的信息后 -------------
Games
Games

后果剖析:两个援用 student1 和 student2 指向不同的两个对象,然而两个援用 student1 和 student2 中的两个 teacher 援用指向的是同一个对象,所以阐明是浅拷贝。

三、深拷贝

(1)、定义

深拷贝是一个整个独立的对象拷贝,深拷贝会拷贝所有的属性, 并拷贝属性指向的动态分配的内存。当对象和它所援用的对象一起拷贝时即产生深拷贝。深拷贝相比于浅拷贝速度较慢并且花销较大。

简而言之,深拷贝把要复制的对象所援用的对象都复制了一遍。

(2)、深拷贝实例

package com.test;

public class DeepCopy {public static void main(String[] args) throws CloneNotSupportedException {Teacher2 teacher = new Teacher2();
        teacher.setName("riemann");
        teacher.setAge(27);

        Student3 student1 = new Student3();
        student1.setName("edgar");
        student1.setAge(18);
        student1.setTeacher(teacher);

        Student3 student2 = (Student3) student1.clone();
        System.out.println("拷贝后");
        System.out.println(student2.getName());
        System.out.println(student2.getAge());
        System.out.println(student2.getTeacher().getName());
        System.out.println(student2.getTeacher().getAge());

        System.out.println("批改老师的信息后 -------------");
        // 批改老师的信息
        teacher.setName("Games");
        System.out.println(student1.getTeacher().getName());
        System.out.println(student2.getTeacher().getName());
    }
}

class Teacher2 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;}

    public Object clone() throws CloneNotSupportedException {return super.clone();
    }
}

class Student3 implements Cloneable {
    private String name;
    private int age;
    private Teacher2 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 Teacher2 getTeacher() {return teacher;}

    public void setTeacher(Teacher2 teacher) {this.teacher = teacher;}

    public Object clone() throws CloneNotSupportedException {
        // 浅复制时:// Object object = super.clone();
        // return object;

        // 改为深复制:Student3 student = (Student3) super.clone();
        // 原本是浅复制,当初将 Teacher 对象复制一份并从新 set 进来
        student.setTeacher((Teacher2) student.getTeacher().clone());
        return student;

    }
}

输入后果:

 拷贝后
edgar
18
riemann
27
批改老师的信息后 -------------
Games
riemann

后果剖析:

两个援用 student1 和 student2 指向不同的两个对象,两个援用 student1 和 student2 中的两个 teacher 援用指向的是两个对象,但对 teacher 对象的批改只能影响 student1 对象, 所以说是深拷贝。

对于 Java 深拷贝和浅拷贝区别,你学废了么?


正文完
 0