深浅克隆

Posted jiminluo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深浅克隆相关的知识,希望对你有一定的参考价值。

package com.gxnu.edu.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.gxnu.edu.lqm.Month;

public class Person implements Comparable<Person>,Cloneable,Serializable{

private String name;
private double height;
private Month month;
private List<Integer> list;

{
list = new ArrayList<>();
list.addAll(Arrays.asList(1,2,3,4));
}

public void add(int i){
this.list.add(i);
}

public void printList(){
list.forEach(System.out::println);
}


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, double height, Month month) {
super();
this.name = name;
this.height = height;
this.month = month;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", month=" + month + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((month == null) ? 0 : month.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
if (month != other.month)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Person p) {

int i = this.getName().compareTo(p.getName());
if(i==0){
double d = this.getHeight()-p.getHeight();
if(d==0){
i=this.getMonth().ordinal()-p.getMonth().ordinal();
}else if(d<0){
i=-1;
}else{
i=1;
}
}
return i;
}
public Person clone(){
try {
return (Person) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

}

 

 

package com.gxnu.edu.lqm.collection;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.gxnu.edu.bean.Person;

public class PersonClone {
public static Person deepClone(Person p) {
Person pClone = null;
try(
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/person.abc"));
){
oos.writeObject(p);

}catch(Exception e){
e.printStackTrace();
}
try(
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/person.abc"));
){
pClone = (Person) ois.readObject();

}catch(Exception e){
e.printStackTrace();
}
return pClone;
}

public static Person deepCloneGood(Person p) {
Person pClone = null;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
try(
ObjectOutputStream oos = new ObjectOutputStream(bao);
){
oos.writeObject(p);

}catch(Exception e){
e.printStackTrace();
}
byte[] arr = bao.toByteArray();
try(
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(arr));
){
pClone = (Person) ois.readObject();

}catch(Exception e){
e.printStackTrace();
}
return pClone;
}

}

 

 

package com.gxnu.edu.lqm.collection.test;

import org.junit.Test;

import com.gxnu.edu.bean.Person;
import com.gxnu.edu.lqm.Month;
import com.gxnu.edu.lqm.collection.PersonClone;

public class PersonCloneTest {
@Test
public void testClone(){
Person p = new Person("aa",166,Month.APRIL);
Person p2 = p.clone();
p2.add(10);
p2.add(12);
System.out.println("*******原来对象的list:*******");
p.printList();
System.out.println("*******克隆对象的list******");
p2.printList();

System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");

p = new Person("aa",166,Month.APRIL);
p2 = PersonClone.deepClone(p);
p2.add(10);
p2.add(12);
System.out.println("*******原来对象的list:*******");
p.printList();
System.out.println("*******克隆对象的list******");
p2.printList();


System.out.println("+++++++++++++++++++++++++");
p = new Person("aa",166,Month.APRIL);

long start1 = System.nanoTime();
p2 = PersonClone.deepClone(p);
long use1 = System.nanoTime()-start1;
System.out.println("外存深克隆耗时:"+use1);

long start = System.nanoTime();
p2 = PersonClone.deepCloneGood(p);
long use = System.nanoTime()-start;
System.out.println("内存深克隆耗时:"+use);
p2.add(10);
p2.add(12);
System.out.println("*******原来对象的list:*******");
p.printList();
System.out.println("*******克隆对象的list******");
p2.printList();

}

}



































































































































































































以上是关于深浅克隆的主要内容,如果未能解决你的问题,请参考以下文章

深浅克隆和Promise异步编程

从克隆(深浅拷贝)到原型设计模式

[记录] JavaScript 中的深浅拷贝(克隆)

Java中对象的深浅克隆之序列化篇

java中深克隆与浅克隆的区别

JS----对象的合并与克隆与数组的深浅克隆