Java 浅拷贝和深拷贝
2015-09-15 19:00
330 查看
浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用.
深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
1、浅拷贝:
结果:
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();
}
}
深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统