Java浅拷贝与深拷贝(思维导图)

Java浅拷贝与深拷贝(思维导图)

图1 拷贝思维导图(点击查看图片)

1,拷贝

  有两个相同属性的对象A和B,A拥有初始化值,将其值拷贝到B中,使得B拥有与A“相同”数据的属性!注意这里的相同我有加双引号!

  相同可能表示这么几个意思:①数值相同【指向不同的内存空间】;②地址相同【指向相同的内存空间】;

  下面是直接使用"="进行复制的操作

 package com.cnblogs.mufasa.Demo1;

 import java.util.Date;

 class Person{//
private int age=0;
private Birth birth=new Birth();
private String name=""; public Person(int age, Birth birth, String name) {
this.age = age;
this.birth = birth;
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Birth getBirth() {
return birth;
} public void setBirth(Birth birth) {
this.birth = birth;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+name+",年龄:"+age+",出生日期:"+birth.toString();
}
} class Birth{//生日
private String date="";
public Birth(){}
public Birth(String date){
this.date=date;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} @Override
public String toString() {
return this.date;
}
} public class Demo1 {//浅拷贝
public static void main(String[] args) {
Person p1=new Person(18,new Birth("19950729"),"万雨");
Person p2=p1;
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(new Birth("2018"));
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}
姓名:万雨,年龄:18,出生日期:19950729
姓名:万雨,年龄:18,出生日期:19950729
姓名:Mufasa,年龄:17,出生日期:2018
姓名:Mufasa,年龄:17,出生日期:2018

  其实就相当于对原始对象进行操作

2,浅拷贝

  Perosn类型对象有两个属性Age、name,将p1浅复制给p2

  基本类型直接进行数值复制,引用类型进行地址拷贝(String类型比较特殊,属于引用类型,但是它存在一个常量池需要进行特殊对待)

Java浅拷贝与深拷贝(思维导图)

图2 浅拷贝

使用构造方法进行浅拷贝:

 package com.cnblogs.mufasa.Demo;

 import java.util.Date;

 class Person{//
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Person(Person p1){
this.age=p1.age;
this.name=p1.name;
this.birth=p1.birth;
} public int getAge() {
return this.age.getAge();
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age{
private int age;
public Age(){}
public Age(int age){
this.age=age;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo {//浅拷贝-构造方法实现
public static void main(String[] args) {
Person p1=new Person(new Age(18),"万雨",1995);
Person p2=new Person(p1);
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}

使用继承Cloneable接口调用clone方法进行浅拷贝

 package com.cnblogs.mufasa.Demo2;

 import java.util.Date;

 class Person implements Cloneable{//
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Person clone(){
Person obj=null;
try {
obj=(Person) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
// obj.age=(Age) this.getAge().clone();
return obj;
} public Age getAge() {
return this.age;
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age implements Cloneable{
private int age;
public Age(){}
public Age(int age){
this.age=age;
}
// public Age clone(){
// Age obj=null;
// try {
// obj=(Age) super.clone();
// }catch (CloneNotSupportedException e){
// e.printStackTrace();
// }
// return obj;
// } public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo2 {//浅拷贝-继承Cloneable接口实现
public static void main(String[] args) {
Person p1=new Person(new Age(18),"万雨",1995);
Person p2=(Person)p1.clone();
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}
姓名:万雨,年龄:18,出生日期:1995
姓名:万雨,年龄:18,出生日期:1995
姓名:Mufasa,年龄:17,出生日期:2019
姓名:万雨,年龄:17,出生日期:1995

3,深拷贝

  引用类型数据也进行新内存开辟与幅值,

Java浅拷贝与深拷贝(思维导图)

图3 深拷贝

使用继承Cloneable接口调用clone方法进行深拷贝【每个引用对象都需要使用clone方法进行拷贝】

 package com.cnblogs.mufasa.Demo2;

 import java.util.Date;

 class Person implements Cloneable{//
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Person clone(){
Person obj=null;
try {
obj=(Person) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
obj.age=(Age) this.getAge().clone();
return obj;
} public Age getAge() {
return this.age;
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age implements Cloneable{
private int age;
public Age(){}
public Age(int age){
this.age=age;
}
public Age clone(){
Age obj=null;
try {
obj=(Age) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
return obj;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo2 {//浅拷贝-继承Cloneable接口实现
public static void main(String[] args) {
Person p1=new Person(new Age(18),"万雨",1995);
Person p2=(Person)p1.clone();
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}

使用继承Serializable接口进行序列化与反序列化进行深拷贝【注意对象不能使用transient进行修饰,原因:transient修饰的对象无法进行序列化!】

 package com.cnblogs.mufasa.Demo3;

 import java.io.*;

 class Person implements Serializable{
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Age getAge() {
return this.age;
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age implements Serializable {
private int age;
public Age(){}
public Age(int age){
this.age=age;
}
public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo3 {//浅拷贝-继承Cloneable接口实现
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person p1=new Person(new Age(18),"万雨",1995); ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos); oos.writeObject(p1);//1,先进行序列化
oos.flush();
ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
Person p2=(Person)ois.readObject();//2,进行反序列化 System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}
姓名:万雨,年龄:18,出生日期:1995
姓名:万雨,年龄:18,出生日期:1995
姓名:Mufasa,年龄:17,出生日期:2019
姓名:万雨,年龄:18,出生日期:1995
上一篇:Java 浅拷贝,深拷贝


下一篇:浅谈java浅拷贝和深拷贝