java中通过泛型和反射实现类拷贝
2014-07-02 00:00
344 查看
public <T> T CopyT(Class<T> t, T template) { T model = null; try { model = t.newInstance(); Class<?> classType = template.getClass(); Field[] flds = classType.getFields(); for( Field fld : flds) { try { Object value = fld.get(template); fld.set(model, value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } return model; } //简单点的通过反射实现拷贝 public Object CopyObj(Object OldObj) { Object newObj = new Object(); Class<?> classType = OldObj.getClass(); Field[] flds = classType.getFields(); for( Field fld : flds) { try { Object value = fld.get(OldObj); fld.set(newObj, value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return newObj; }
相关文章推荐
- 通过Java的反射机制实现对象的拷贝
- Java通过反射实现简单对象的拷贝
- java_通过反射获得泛型的实际类型参数
- 通过java反射实现对javabean生成各种sql语句
- java 通过反射获取泛型的类型
- java 通过反射获取泛型的类型
- java通过反射获取List中的泛型
- java 通过反射获取泛型的类型
- java基础-反射 --通过反射 获取泛型实际类型参数
- java如何通过反射取得泛型的实例化对象
- java通过反射实现数据库的编写
- 通过JAVA反射获取父类的泛型
- java通过反射,泛型将定义的对象与对象之间互转
- java 通过反射获取泛型的类型
- java 通过反射获取泛型的类型
- java 通过反射获取泛型的类型
- 黑马程序员--Java基础加强--15.利用反射操作泛型IV【通过反射Method解析泛型方法思路】【通过Method对四种Type子接口类型进行解剖】【使用递归对任意复合泛型类型进行彻底解剖】【个人
- 黑马程序员--Java基础加强--16.利用反射操作泛型V【通过Constructor反射解析泛型构造方法】【通过Field反射解析泛型成员变量】【个人总结】
- Java 泛型学习(三)通过反射获得泛型的实际类型参数
- 通过反射和泛型实现DAO层的基类