import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TestCopy {
public static void main(String[] args) throws Exception{
Person p1=new Person("sunyuhua",22);
p1.setAddress("山东省","潍坊市");
System.out.println(p1);
System.out.println(p1.getPname().hashCode());
Person p2=(Person)p1.clone();
System.out.println(p2);
System.out.println(p2.getPname().hashCode());
System.out.println(p1==p2);
System.out.println(p1.equals(p2));
p1.display("p1");
p2.display("p2");
p2.setAddress("上海","浦东");
System.out.println(p1.getPname().hashCode());
System.out.println(p2.getPname().hashCode());
System.out.println(p1==p2);
System.out.println(p1.equals(p2));
p1.display("p1");
p2.display("p2");
System.out.println("深拷贝深拷贝深拷贝深拷贝深拷贝深拷贝");
DeepPerson deepPerson1=new DeepPerson("sunyuhua",22);
deepPerson1.setAddress("山东省","潍坊市");
System.out.println(deepPerson1);
System.out.println(deepPerson1.getPname().hashCode());
DeepPerson deepPerson2=(DeepPerson)deepPerson1.clone();
System.out.println(deepPerson2);
System.out.println(deepPerson2.getPname().hashCode());
System.out.println(deepPerson1==deepPerson2);
System.out.println(deepPerson1.equals(deepPerson2));
deepPerson1.display("deepPerson1");
deepPerson2.display("deepPerson2");
deepPerson2.setAddress("上海","浦东");
System.out.println(deepPerson1.getPname().hashCode());
System.out.println(deepPerson2.getPname().hashCode());
System.out.println(deepPerson1==deepPerson2);
System.out.println(deepPerson1.equals(deepPerson2));
p1.display("deepPerson1");
p2.display("deepPerson2");
System.out.println("深拷贝深拷贝深拷贝深拷贝深拷贝深拷贝");
System.out.println("利用序列化利用序列化利用序列化利用序列化");
Person person1=new Person("sunyuhua",22);
person1.setAddress("山东省","潍坊市");
System.out.println(person1);
System.out.println(person1.getPname().hashCode());
Person person2=(Person) deepClone(person1);
System.out.println(person2);
System.out.println(person2.getPname().hashCode());
System.out.println(person1==person2);
System.out.println(person1.equals(person2));
deepPerson1.display("person1");
deepPerson2.display("person2");
deepPerson2.setAddress("上海","浦东");
System.out.println(person1.getPname().hashCode());
System.out.println(person2.getPname().hashCode());
System.out.println(person1==person2);
System.out.println(person1.equals(person2));
p1.display("person1");
p2.display("person2");
}
public static Object deepClone(Person person) throws Exception{
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(person);
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
return objectInputStream.readObject();
}
}
@Data
public class Person implements Cloneable, Serializable {
public String pname;
public int page;
public Address address;
public Person(String pname,int page){
this.pname=pname;
this.page=page;
this.address=new Address();
}
public void setAddress(String provices,String city){
address.setAddress(provices,city);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public void display(String name){
System.out.println("name="+pname+",page="+page+"address="+address.toString());
}
}
package com.chen.www.chendemo.objectCopy;
import java.io.Serializable;
public class Address implements Serializable {
private String provices;
private String city;
public void setAddress(String provices,String city){
this.provices=provices;
this.city=city;
}
@Override
public String toString() {
return "address==provices="+provices+"city="+city;
}
}
public class DeepAddress implements Cloneable{
private String provices;
private String city;
public void setAddress(String provices,String city){
this.provices=provices;
this.city=city;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "address==provices="+provices+"city="+city;
}
}
import lombok.Data;
@Data
public class DeepPerson implements Cloneable{
public String pname;
public int page;
public DeepAddress deepAddress;
public DeepPerson(String pname,int page){
this.pname=pname;
this.page=page;
this.deepAddress=new DeepAddress();
}
public void setAddress(String provices,String city){
deepAddress.setAddress(provices,city);
}
@Override
protected Object clone() throws CloneNotSupportedException {
DeepPerson person=(DeepPerson)super.clone();
person.deepAddress=(DeepAddress)deepAddress.clone();
return person;
}
public void display(String name){
System.out.println("name="+pname+",page="+page+"address="+deepAddress.toString());
}
}