Java中的浅拷贝与深拷贝
Java中的浅拷贝与深拷贝
1.浅拷贝含义:
对于基本类型来讲都是值的引用,但是对于引用类型来讲,拷贝的是同一个引用,也就是同一个首地址的对象,它所指的堆内存都是一样的。
2.深拷贝含义:
对于基本类型来讲都是值的引用,但是对于引用类型来讲,会再创建一个新的对象,指向另一个首地址。
班级类拷贝方法为浅拷贝
public class Classes implements Cloneable{
/*
学生
*/
private Student student;/
班级名称
*/
private String classesName;/班级编号
/
private String classesCode;public Classes(Student student, String classesName, String classesCode) {
super();
this.student = student;
this.classesName = classesName;
this.classesCode = classesCode;
}public Student getStudent() {
return student;
}public void setStudent(Student student) {
this.student = student;
}public String getClassesName() {
return classesName;
}public void setClassesName(String classesName) {
this.classesName = classesName;
}public String getClassesCode() {
return classesCode;
}public void setClassesCode(String classesCode) {
this.classesCode = classesCode;
}public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
/**
- 学生类
- @author win 10
/
public class Student implements Cloneable{
/- 姓名
*/
private String name;
/*
- 年龄
*/
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = 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 class TestClone {
public static void main(String[] args) {
Student stu = new Student("张三",16);
Classes cla = new Classes(stu,"初一(2)班","002");
System.out.println("原对象:"+cla);
System.out.println("原对象student属性"+stu);
System.out.println("姓名:"+cla.getStudent().getName()+"年龄:"+cla.getStudent().getAge());
try {
Classes cla2 = (Classes)cla.clone();
//重新设置拷贝对象student的年龄属性,用于观察是否会影响到原对象的student年龄属性
cla2.getStudent().setAge(18);
System.out.println("拷贝对象:"+cla2);
System.out.println("拷贝对象student属性:"+cla2.getStudent());
System.out.println("拷贝对象姓名:"+cla2.getStudent().getName()+"年龄:"+cla2.getStudent().getAge());
System.out.println("原对象姓名:"+cla.getStudent().getName()+"年龄:"+cla.getStudent().getAge());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
测试结果:
虽然Classes对象已经是深拷贝,但是Classes对象里的Student对象并没有做到深拷贝。拷贝出来的Classes对象的Student属性会跟随着原Classes的Student的变化而变化同样的原Classes的Student也会跟着拷贝后的Student变化后变化。
改版后的代码:
package Clone;
/**
- 班级类
- @author win 10
/
public class Classes implements Cloneable{
/- 学生
*/
private Student student;
/
班级名称
*/
private String classesName;/班级编号
/
private String classesCode;public Classes(Student student, String classesName, String classesCode) {
super();
this.student = student;
this.classesName = classesName;
this.classesCode = classesCode;
}public Student getStudent() {
return student;
}public void setStudent(Student student) {
this.student = student;
}public String getClassesName() {
return classesName;
}public void setClassesName(String classesName) {
this.classesName = classesName;
}public String getClassesCode() {
return classesCode;
}public void setClassesCode(String classesCode) {
this.classesCode = classesCode;
}public Object clone() throws CloneNotSupportedException {
Classes cla = (Classes)super.clone();
cla.student = (Student)student.clone();
return cla;
}
}
将Stundent引用类型也做一个深拷贝- 学生
package Clone;
/**
- 学生类
- @author win 10
/
public class Student implements Cloneable{
/- 姓名
*/
private String name;
/年龄
/
private int age;public Student(String name, int age) {
super();
this.name = name;
this.age = 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();
}
}
Student也加上拷贝方法
测试结果:- 姓名
更多技术分享,请关注微信公众号 Big Data课堂