java 反射实现深拷贝_一种c#深拷贝方式完胜java深拷贝(实现上的对比)

楼主是一名asp.net攻城狮,最近经常跑java组客串帮忙开发,所以最近对java的一些基础知识特别上心。却遇到需要将一个对象深拷贝出来做其他事情,而原对象保持原有状态的情况。(实在是不想自己new一个出来,然后对着一堆字段赋值......好吧,再此之前我没有关心是否项目框架有深拷贝的方法),然后就想着用反射实现吧....接下来

4e8a9e25a2320488f6652e983c27ccdd.png

是我自己的原因,还是真的不存在这样的纯用反射实现的深拷贝方式....(c#是有纯反射实现的)

但也不能算自己白忙活吧,也找到了其他实现深拷贝的方式(但是每种方式我都觉得并不是太合理,也许是因为c#的方式带入了吧,最后贴出c#版本纯反射实现深拷贝的代码)

先说java的深拷贝方式0.0

java深拷贝方式一:实现Cloneable接口,重写clone方法

实体类:一个轮胎类,一个车辆类,车辆中包含轮胎

1 /**轮胎类**/

2 public class Tire implementsCloneable {3 publicString color;4 public intradius;5 publicTire(){}6 public Tire(String color, intradius) {7 this.color =color;8 this.radius =radius;9 }10

11 @Override12 protected Object clone() throwsCloneNotSupportedException {13 return super.clone();14 }15 }16 /**车辆类**/

17 public class Car implementsCloneable{18 publicString name;19 publicString color;20 publicTire tire;21 publicCar() {}22 publicCar(String name, String color, Tire tire) {23 this.name =name;24 this.color =color;25 this.tire =tire;26 }27 public voidwhistle(){28 System.out.println("汽车"+this.name+" 鸣笛...");29 }30 publicString getName() {31 returnname;32 }33 public voidsetName(String name) {34 this.name =name;35 }36 publicString getColor() {37 returncolor;38 }39 public voidsetColor(String color) {40 this.color =color;41 }42 publicTire getTire() {43 returntire;44 }45 public voidsetTire(Tire tire) {46 this.tire =tire;47 }48 @Override49 protected Object clone() throwsCloneNotSupportedException {50 return super.clone();51 }52 }

单元测试:

1 @Test2 public void test() throwsCloneNotSupportedException {3 Tire tire = new Tire("black",100);4 Car car = new Car("奔驰","white",tire);5 Car car_copy =(Car)car.clone();6 System.out.println("car:"+car.hashCode()+" car.tire:"+car.tire.hashCode());7 System.out.println("car_copy:"+car_copy.hashCode()+" car_copy.tire:"+car_copy.tire.hashCode());8 car_copy.color = "blue";9 System.out.println("car_copy:"+car_copy.color+" car:"+car.color);10 }

输出结果:

car:1223737555 car.tire:906199566

car_copy:542081238 car_copy.tire:906199566

car_copy:blue car:white

从结果可以的之,car与car_copy的内存地址并不一致,但car.tire与car_copy.tire的内存地址却是一致的,说明“奔驰”车确实又造出了一辆,但却公用同一幅轮胎(这种情形....哈哈哈),好吧,也就是只复制了tire的引用,这可以说是深拷贝的不彻底 (hashCode()的值可以当作是内存地址来理解),那么要怎样才能彻底,真正的深拷贝?

修改Car类中的clone方法:

1 @Override2 protected Object clone() throwsCloneNotSupportedException {3 Car car = (Car)super.clone();4 car.tire =(Tire)car.tire.clone();5 returncar;6 }

输出结果:

car:1223737555 car.tire:906199566

car_copy:542081238 car_copy.tire:1133736492

car_copy:blue car:white

这样最终实现了,但这种方式用到项目中并不是很合适吧,每个需要深拷贝的类,都要实现Cloneable接口,并覆盖其clone方法,遇到引用其他类时候更是需要修改clone方法,要是引用其他类,其他类再引用其他类呢?这不好吧......

java深拷贝方式二:通过序列化与反序列化实现(实现Serializable接口)

实体类:与第一种方式类似,换成实现Serializable接口,去掉clone方法

/**轮胎类**/@SuppressWarnings("serial")public class Tire implementsjava.io.Serializable {publicString color;public intradius;publicTire(){}public Tire(String color, intradius) {this.color =color;this.radius =radius;

}

}/**车辆类**/@SuppressWarnings("serial")public class Car implementsjava.io.Serializable{publicString name;publicString color;publicTire tire;publicCar() {}publicCar(String name, String color, Tire tire) {this.name =name;this.color =color;this.tire =tire;

}public voidwhistle(){

System.out.println("汽车"+this.name+" 鸣笛...");

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getColor() {returncolor;

}public voidsetColor(String color) {this.color =color;

}publicTire getTire() {returntire;

}public voidsetTire(Tire tire) {this.tire =tire;

}

}

深拷贝方法:

1 @SuppressWarnings("unchecked")2 public staticObject deepClone(Object obj)3 {4 Object copyObj = null;5 ObjectOutputStream out = null;6 ObjectInputStream in = null;7 try{8 //序列化

9 ByteArrayOutputStream bufferOut = newByteArrayOutputStream();10 out = newObjectOutputStream(bufferOut);11

12 out.writeObject(obj);13

14 //反序列化

15 ByteArrayInputStream bufferIn = newByteArrayInputStream(bufferOut.toByteArray());16 in = newObjectInputStream(bufferIn);17 copyObj =in.readObject();18 } catch(Exception e) {19 e.printStackTrace();20 throw newRuntimeException(e);21 }finally{22 try{23 if(in != null){24 in.close();25 }26 if(out!=null){27 out.close();28 }29 }catch(IOException e){30 throw newRuntimeException(e);31 }32 }33 returncopyObj;34 }

单元测试:

1 @Test2 public void test() throwsCloneNotSupportedException {3 Tire tire = new Tire("black",100);4 Car car = new Car("奔驰","white",tire);5 Car car_copy =(Car)deepClone(car);6 System.out.println("car:"+car.hashCode()+" car.tire:"+car.tire.hashCode());7 System.out.println("car_copy:"+car_copy.hashCode()+" car_copy.tire:"+car_copy.tire.hashCode());8 car_copy.color = "blue";9 System.out.println("car_copy:"+car_copy.color+" car:"+car.color);10 }

输出结果:

car:2019524978 car.tire:855703640

car_copy:1407965019 car_copy.tire:545768040

car_copy:blue car:white

从结果集中可以看出是深拷贝是正确的,但是每个类还是需要实现Serializable,好像也不合适吧......

优化一下深拷贝方法:将其换成泛型,这样拷贝出来就不需要强转了(好吧,其实也没比上面的方法好到哪去...)

1 @SuppressWarnings("unchecked")2 public static T deepClone(T obj)3 {4 T copyObj = null;5 ObjectOutputStream out = null;6 ObjectInputStream in = null;7 try{8 //序列化

9 ByteArrayOutputStream bufferOut = newByteArrayOutputStream();10 out = newObjectOutputStream(bufferOut);11

12 out.writeObject(obj);13

14 //反序列化

15 ByteArrayInputStream bufferIn = newByteArrayInputStream(bufferOut.toByteArray());16 in = newObjectInputStream(bufferIn);17 copyObj =(T)in.readObject();18 } catch(Exception e) {19 e.printStackTrace();20 throw newRuntimeException(e);21 }finally{22 try{23 if(in != null){24 in.close();25 }26 if(out!=null){27 out.close();28 }29 }catch(IOException e){30 throw newRuntimeException(e);31 }32 }33 returncopyObj;34 }

通过序列化与反序列化深拷贝还有更简单的实现方式,就是需要导个包(拷贝的类也必须实现Serializable接口),当然,我已经为你们准备好了 点击->

深拷贝方法:就一行代码...

1 publicObject deepClone(Object obj){2 returnorg.apache.commons.lang.SerializationUtils.clone((Serializable)obj);3 }

好了,java的暂时就到这里了,当然对于这两种方式并不是很满意...

-------------------------------------------------

C#深拷贝 反射实现

下面方法是c#的深拷贝,纯反射实现,无需实现任何接口,哦对,需要实体类有个无参的构造方法,简单使用强大,微软大法好啊......有需要用到的同学就拿去用吧,目前经过一个几百W的项目框架中考验,真的强大实用

1 ///

2 ///对象拷贝3 ///

4 /// 被复制对象

5 /// 新对象

6 private object CopyOjbect(objectobj) {7 if (obj == null) {8 return null;9 }10 Object targetDeepCopyObj;11 Type targetType =obj.GetType();12 //值类型

13 if (targetType.IsValueType == true) {14 targetDeepCopyObj =obj;15 }16 //引用类型

17 else{18 targetDeepCopyObj = System.Activator.CreateInstance(targetType); //创建引用对象

19 System.Reflection.MemberInfo[] memberCollection =obj.GetType().GetMembers();20

21 foreach (System.Reflection.MemberInfo member inmemberCollection) {22 //拷贝字段

23 if (member.MemberType ==System.Reflection.MemberTypes.Field)24 {25 System.Reflection.FieldInfo field =(System.Reflection.FieldInfo)member;26 Object fieldValue =field.GetValue(obj);27 if (fieldValue isICloneable)28 {29 field.SetValue(targetDeepCopyObj, (fieldValue asICloneable).Clone());30 }31 else

32 {33 field.SetValue(targetDeepCopyObj, CopyOjbect(fieldValue));34 }35

36 }//拷贝属性

37 else if (member.MemberType ==System.Reflection.MemberTypes.Property) {38 System.Reflection.PropertyInfo myProperty =(System.Reflection.PropertyInfo)member;39

40 MethodInfo info = myProperty.GetSetMethod(false);41 if (info != null) {42 try{43 object propertyValue = myProperty.GetValue(obj, null);44 if (propertyValue isICloneable) {45 myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);46 }47 else{48 myProperty.SetValue(targetDeepCopyObj, CopyOjbect(propertyValue), null);49 }50 }51 catch(System.Exception ex) {52

53 }54 }55 }56 }57 }58 returntargetDeepCopyObj;59 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值