十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Java中的浅拷贝与深拷贝
成都创新互联长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为资兴企业提供专业的网站建设、成都做网站,资兴网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
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();
}
}
/**
/
public class Student implements Cloneable{
/
/*
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;
/**
/
public class Classes implements Cloneable{
/
/
班级名称
*/
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;
/**
/
public class Student implements Cloneable{
/
/年龄
/
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课堂