您的位置:首页 > 编程语言 > Java开发

Java 浅拷贝和深拷贝

2015-09-15 19:00 330 查看
浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用.
深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.

1、浅拷贝:

public class ShallowCopy {
public static void main(String[] args) {
Information information = new Information(123);
Person1 s1 = new Person1("hello", 25, information);
Person1 s2 = null;
s2 = (Person1) s1.clone();
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
s2.getInfo().setTel(12345);
s2.setName("test");
System.out.println(s1.getInfo().getTel() + " " + s1.getName());
System.out.println(s2.getInfo().getTel() + " " + s2.getName());
}
}

class Information {
private int tel;

public Information(int tel) {
// TODO Auto-generated constructor stub
this.setTel(tel);
}

public int getTel() {
return tel;
}

public void setTel(int tel) {
this.tel = tel;
}
}

class Person1 implements Cloneable {
private String name;
private int age;
private Information info;

public Person1(String name, int age, Information info) {
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.info = info;
}

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;
}

@Override
protected Object clone() {
// TODO Auto-generated method stub
Person1 o = null;
try {
o = (Person1) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}

@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (!(obj instanceof Person1))
return false;
Person1 p = (Person1) obj;
if (!p.name.equals(this.name))
return false;
if(p.age != this.age)
return false;
return true;
}

public Information getInfo() {
return info;
}

public void setInfo(Information info) {
this.info = info;
}

}

结果:

false

true

12345 hello

12345 test

2、深拷贝

public class DeepCopy {
public static void main(String[] args) {
Information information = new Information(123);
Person1 s1 = new Person1("hello", 25, information);
Person1 s2 = null;
s2 = (Person1) s1.clone();
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
s2.getInfo().setTel(12345);
s2.setName("test");
System.out.println(s1.getInfo().getTel() + " " + s1.getName());
System.out.println(s2.getInfo().getTel() + " " + s2.getName());
}
}

class Information implements Cloneable {
private int tel;

public Information(int tel) {
// TODO Auto-generated constructor stub
this.setTel(tel);
}

public int getTel() {
return tel;
}

public void setTel(int tel) {
this.tel = tel;
}

@Override
protected Object clone() {
// TODO Auto-generated method stub
Information o = null;
try {
o = (Information) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}

}

class Person1 implements Cloneable {
private String name;
private int age;
private Information info;

public Person1(String name, int age, Information info) {
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.info = info;
}

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;
}

@Override
protected Object clone() {
// TODO Auto-generated method stub
Person1 o = null;
try {
o = (Person1) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
o.info = (Information) info.clone();
return o;
}

@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (!(obj instanceof Person1))
return false;
Person1 p = (Person1) obj;
if (!p.name.equals(this.name))
return false;
if(p.age != this.age)
return false;
return true;
}

public Information getInfo() {
return info;
}

public void setInfo(Information info) {
this.info = info;
}

}结果:
false

true

123 hello

12345 test

3、流深拷贝

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class StreamCopy {

public static void main(String[] args) throws ClassNotFoundException, IOException {
Information information = new Information(123);
Person1 s1 = new Person1("hello", 25, information);
Person1 s2 = null;
s2 = (Person1) s1.StreamDeepCopy();
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
s2.getInfo().setTel(12345);
s2.setName("test");
System.out.println(s1.getInfo().getTel() + " " + s1.getName());
System.out.println(s2.getInfo().getTel() + " " + s2.getName());
}
}

class Information implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int tel;

public Information(int tel) {
// TODO Auto-generated constructor stub
this.setTel(tel);
}

public int getTel() {
return tel;
}

public void setTel(int tel) {
this.tel = tel;
}
}

class Person1 implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private Information info;

public Person1(String name, int age, Information info) {
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.info = info;
}

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;
}

@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (!(obj instanceof Person1))
return false;
Person1 p = (Person1) obj;
if (!p.name.equals(this.name))
return false;
if (p.age != this.age)
return false;
return true;
}

public Information getInfo() {
return info;
}

public void setInfo(Information info) {
this.info = info;
}

public Object StreamDeepCopy() throws IOException, ClassNotFoundException{
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(this);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return oi.readObject();
}

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