[color=indigo][color=indigo][color=indigo]public class Student{
private Integer sex ;
private String typeName ="good";
private Date sysdate ;
...
}
public class TestcopyBean{
public static Student getMap() {[color=indigo][/color]
Student map = new Student();
map.setSex(new Integer(1));
map.setTypeName( "hao");
map.setSysdate(new Date());
return map;
}
public static void merge(Student map)
throws SecurityException, NoSuchFieldException,
NoSuchMethodException, IllegalArgumentException,
InvocationTargetException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Class c=map.getClass();
Field[] fields = c.getDeclaredFields();//得到此类所有的字段
//根据传入对象的类型,构造一个新的对象
Object o = c.newInstance();
Method setmethod = null;
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
String setMethod = "set" + fieldName.substring(0, 1).toUpperCase() +
fieldName.substring(1);//根据字段名拼凑其set方法名
String getMethod = "get"+ fieldName.substring(0, 1).toUpperCase() +
fieldName.substring(1);
//根据方法的名字,取得Method对象,下面两行语句分别取得了get和set方法对象
setmethod = c.getDeclaredMethod(setMethod,new Class[]{fields[i].getType()});
Method getMothod=c.getMethod(getMethod, new Class[]{});
Object value=getMothod.invoke(map, new Object[]{});
setmethod.invoke(o, new Object[]{value});
}
Student beano = (Student)o;
System.out.println(beano.getSex()+" "+beano.getTypeName()+" "+beano.getSysdate());
}
public static void main(String[] args) throws DocumentException, SecurityException, IllegalArgumentException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, InstantiationException, IllegalAccessException{
merge(getMap());
}[/color][/color][/color]
private Integer sex ;
private String typeName ="good";
private Date sysdate ;
...
}
public class TestcopyBean{
public static Student getMap() {[color=indigo][/color]
Student map = new Student();
map.setSex(new Integer(1));
map.setTypeName( "hao");
map.setSysdate(new Date());
return map;
}
public static void merge(Student map)
throws SecurityException, NoSuchFieldException,
NoSuchMethodException, IllegalArgumentException,
InvocationTargetException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Class c=map.getClass();
Field[] fields = c.getDeclaredFields();//得到此类所有的字段
//根据传入对象的类型,构造一个新的对象
Object o = c.newInstance();
Method setmethod = null;
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
String setMethod = "set" + fieldName.substring(0, 1).toUpperCase() +
fieldName.substring(1);//根据字段名拼凑其set方法名
String getMethod = "get"+ fieldName.substring(0, 1).toUpperCase() +
fieldName.substring(1);
//根据方法的名字,取得Method对象,下面两行语句分别取得了get和set方法对象
setmethod = c.getDeclaredMethod(setMethod,new Class[]{fields[i].getType()});
Method getMothod=c.getMethod(getMethod, new Class[]{});
Object value=getMothod.invoke(map, new Object[]{});
setmethod.invoke(o, new Object[]{value});
}
Student beano = (Student)o;
System.out.println(beano.getSex()+" "+beano.getTypeName()+" "+beano.getSysdate());
}
public static void main(String[] args) throws DocumentException, SecurityException, IllegalArgumentException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, InstantiationException, IllegalAccessException{
merge(getMap());
}[/color][/color][/color]