复制类的类方法
来源:互联网 发布:万网云新建数据库 编辑:程序博客网 时间:2024/06/01 16:53
public Object copy(Object object) throws Exception {
Class<?> classType = object.getClass();
Object objectCopy = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
Field[] fields = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
Method getMethod = classType.getMethod(getMethodName,new Class[] {});
Method setMethod = classType.getMethod(setMethodName,new Class[] { field.getType() });
Object value = getMethod.invoke(object, new Object[] {});
setMethod.invoke(objectCopy, new Object[] { value });
}
return objectCopy;
}
Class<?> classType = object.getClass();
Object objectCopy = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
Field[] fields = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
Method getMethod = classType.getMethod(getMethodName,new Class[] {});
Method setMethod = classType.getMethod(setMethodName,new Class[] { field.getType() });
Object value = getMethod.invoke(object, new Object[] {});
setMethod.invoke(objectCopy, new Object[] { value });
}
return objectCopy;
}
0 0
- 复制类的类方法
- 要素类的复制方法
- Eclipse 下 查询和复制类路径的快捷方法
- File类的构造方法中的多级文件夹复制
- 复制bitmap的方法
- 类的复制控制
- 要素类的复制
- 类对象的复制
- 复制Icon和复制Bitmap的方法
- 利用类库方法实现元素复制
- 类和类之间属性复制,必须存在get和set方法的属性才可以复制
- 复制ArrayCollection变量的方法
- 复制ArrayCollection变量的方法
- 复制ArrayCollection变量的方法
- 复制数据库的另类方法
- 递归的方法复制文件夹
- Python 复制文件的方法
- 删除高级复制的方法
- OpenGL基础渲染
- 常用的SQL语句
- Spring Quartz动态配置时间
- 多级联动实现思路及代码
- Linux 存储结构与磁盘划分
- 复制类的类方法
- 初识、理解适配器模式
- iOS开发从入门到精通--修改app应用的名称,添加AppIcon,LaunchImage
- poj 2236 Wireless Network 并查集
- codeforces 682C. Alyona and the Tree
- URL
- c# sqladapter 与sqldataReader
- C++ 标准模板库
- 支付宝支付