您的位置:首页 > 其它

深复制,浅复制,和对象赋值

2017-11-11 13:25 281 查看
1.对象赋值

package com.template;

class Student implements Cloneable{

private String studentname;

public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}

}

public class Shllowcopy implements Cloneable{

private int a=3;
private int b[]= {1,2,3,4,5};
private Student student;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int[] getB() {
return b;
}

public void setB(int[] b) {
this.b = b;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

int b[]= {6,7,8,9,0};
int k[]= {11,12,13,14,15};
Student student=new Student();

//1.对对象赋值时,赋值对象或被赋值对象的改变,都会导致两个对象发生改变,毕竟两个对象地址都指向了堆中的同一个对象
Shllowcopy shllow=new Shllowcopy();
shllow.setA(6);
shllow.setB(b);
student.setStudentname("苏jbutton");
shllow.setStudent(student);

Shllowcopy shllow1=shllow;

System.out.println(shllow.getA());//6
System.out.println(shllow.getB());//[I@15db9742
System.out.println(shllow.getStudent());//com.template.Student@6d06d69c

System.out.println(shllow1.getA());//6
System.out.println(shllow1.getB());//[I@15db9742
System.out.println(shllow.getStudent());//com.template.Student@6d06d69c

shllow1.setA(7);
shllow1.setB(k);
student.setStudentname("张jbutton");
shllow1.setStudent(student);

System.out.println(shllow1.getA());//7
System.out.println(shllow1.getB());//[I@7852e922
System.out.println(shllow1.getStudent());//com.template.Student@6d06d69c

System.out.println(shllow.getA());//7
System.out.println(shllow.getB());//[I@7852e922
System.out.println(shllow.getStudent());//com.template.Student@6d06d69c

}

}
2.浅复制

package com.template;

class Student implements Cloneable{

private String studentname;

public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}

}

public class Shllowcopy implements Cloneable{

private int a=3;
private int b[]= {1,2,3,4,5};
private Student student;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int[] getB() {
return b;
}

public void setB(int[] b) {
this.b = b;
}

@Override
public Object clone() {

Shllowcopy shllow=null;
try {
if((Shllowcopy)super.clone() instanceof Shllowcopy) {//首先先判断是否克隆强转后的类型是否合法
System.out.println("该类型能强转为Shllowcopy类型的");
shllow = (Shllowcopy)super.clone();

}else {
System.out.println("该类型不能强转为Shllowcopy类型的");
}

} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return shllow;
}

return shllow;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

int b[]= {6,7,8,9,0};
int k[]= {11,12,13,14,15};
Student student=new Student();

//2.重写clone方法使用克隆模式进行浅复制,复制后的两个对象不是同一个
Shllowcopy shllow2=new Shllowcopy();

shllow2.setA(6);
shllow2.setB(b);
student.setStudentname("苏jbutton");
shllow2.setStudent(student);

Shllowcopy shllow3=(Shllowcopy) shllow2.clone();

//这样虽然结果相同,我们不妨试试改变克隆出来的对象
System.out.println(shllow2.getA());//6
System.out.println(shllow2.getB());//[I@15db9742
System.out.println(shllow2.getStudent().getStudentname());//苏jbutton

System.out.println(shllow3.getA());//6
System.out.println(shllow3.getB());//[I@15db9742
System.out.println(shllow3.getStudent().getStudentname());//苏jbutton

//看下面的输出,所以可以看出这里的两个对象不像对象的赋值对象一样指向堆中同一个,而是两个不同的对象
shllow3.setA(7);
shllow3.setB(k);
student.setStudentname("张jbutton");
shllow3.setStudent(student);

System.out.println(shllow3.getA());//7
System.out.println(shllow3.getB());//[I@7852e922
//对引用型变量区别于深复制,浅复制过后的再引用返回给新对象,所以对于student成员变量浅复制后,随便改变其中一个输出结果也相同
System.out.println(shllow3.getStudent().getStudentname());//张jbutton

System.out.println(shllow2.getA());//6
System.out.println(shllow2.getB());//[I@15db9742
//对引用型变量区别于深复制,浅复制过后的再引用返回给新对象,所以对于student成员变量浅复制后,随便改变其中一个输出结果也相同
System.out.println(shllow2.getStudent().getStudentname());//张jbutton

//上面可以看到对象的克隆(浅复制)和对象赋值的本质区别,一个是数据的赋值(对象的地址数据),
//浅拷贝是指在拷贝对象时,对于基本数据类型的变量会重新复制一份,而对于引用类型的变量只是对引用进行拷贝,
//没有对引用指向的对象(student)进行拷贝,只是对引用进行拷贝,所以改变对象改变其中一个引用时影响另外一个对象,所以上面两个student输出名。

}
}

}
3.深复制

package com.template;

class Student implements Cloneable{

private String studentname;

public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}

@Override
public Object clone() {

Student student=null;
try {
if((Student)super.clone() instanceof Student) {//首先先判断是否克隆强转后的类型是否合法
System.out.println("该类型能强转为Student 类型的");
student = (Student)super.clone();
}else {
System.out.println("该类型不能强转为Student 类型的");
}

} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return student;
}

return student;
}

}

public class Shllowcopy implements Cloneable{

private int a=3;
private int b[]= {1,2,3,4,5};
private Student student;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int[] getB() {
return b;
}

public void setB(int[] b) {
this.b = b;
}

@Override
public Object clone() {

Shllowcopy shllow=null;
try {
if((Shllowcopy)super.clone() instanceof Shllowcopy) {//首先先判断是否克隆强转后的类型是否合法
System.out.println("该类型能强转为Shllowcopy类型的");
shllow = (Shllowcopy)super.clone();
//深拷贝,引用变量所指向的对象进行拷贝
student=(Student)student.clone();
}else {
System.out.println("该类型不能强转为Shllowcopy类型的");
}

} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return shllow;
}

return shllow;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

int b[]= {6,7,8,9,0};
int k[]= {11,12,13,14,15};
Student student=new Student();

//4.深拷贝,在拷贝对象的同时也会对对象中引用指向的对象进行拷贝

Shllowcopy shllow2=new Shllowcopy();
shllow2.setA(6);
shllow2.setB(b);
student.setStudentname("苏jbutton");
shllow2.setStudent(student);

Shllowcopy shllow3=(Shllowcopy) shllow2.clone();

System.out.println(shllow2.getA());//6
System.out.println(shllow2.getB());//[I@15db9742
System.out.println(shllow2.getStudent().getStudentname());//苏jbutton

System.out.println(shllow3.getA());//6
System.out.println(shllow3.getB());//[I@15db9742
System.out.println(shllow3.getStudent().getStudentname());//苏jbutton

//下面的输出还是可以看出时两个不同的对象
student.setStudentname("张jbutton");
shllow3.setStudent(student);

System.out.println(shllow3.getA());//7
System.out.println(shllow3.getB());//[I@7852e922
System.out.println(shllow3.getStudent().getStudentname());//张jbutton

System.out.println(shllow2.getA());//6
System.out.println(shllow2.getB());//[I@15db9742
//区别于浅拷贝,就在于是否对  对象中的引用变量所指向的对象进行拷贝
System.out.println(shllow2.getStudent().getStudentname());//苏jbutton

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息