您好,登錄后才能下訂單哦!
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();
}
}
/**
/
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課堂
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。