码迷,mamicode.com
首页 > 其他好文 > 详细

深浅克隆

时间:2018-07-19 21:20:14      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:tcl   oid   pack   The   imp   temp   tput   err   collect   

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();

}

}

深浅克隆

标签:tcl   oid   pack   The   imp   temp   tput   err   collect   

原文地址:https://www.cnblogs.com/jiminluo/p/9337865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!