用反射可以达到要求,但必须设置 get 和 set 方法,即创建 javabean(也可以说是实体类)
但下面的代码限制得更严格一些
1、名称对应,比如 getUserName 对应 setUserName
2、get 方法的返回类型和 set 方法的参数类型对象
3、两个方法的参数个数必须符合 get 和 set 参数个数满足以上条件后,调用 invoke 方法赋值
package FanShe;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class BeanUtils {
private BeanUtils() { } public static synchronized void copyProperties(Object destination,
Object source) {
// 得到两个实体类的类
Class destinationClass = destination.getClass();
Class sourceClass = source.getClass(); // 得到两个类的所有方法
Method[] destinationMethods = destinationClass.getMethods();
Method[] sourceMethods = sourceClass.getMethods(); for (int i = 0; i < destinationMethods.length; i++) {
for (int j = 0; j < sourceMethods.length; j++) {
// 当前正在处理的两个方法
Method currentDestinationMethod = destinationMethods[i];
Method currentSourceMethod = sourceMethods[j]; // 如果两个方法长度小于4,则肯定不是get或set方法
if (currentDestinationMethod.getName().length() < 4
|| currentSourceMethod.getName().length() < 4) {
continue;
} // 源类方法名以get开头,目标类方法名以set开头,否则继续
if (!currentDestinationMethod.getName().startsWith("set")
|| !currentSourceMethod.getName().startsWith("get")) {
continue;
} // 方法名称必须对应
if (!currentDestinationMethod.getName().substring(3).equals(
currentSourceMethod.getName().substring(3))) {
continue;
} // 源类方法参数数量必须为0,否则不是get方法;目标类方法参数数量必须为1,否则不是set方法
Class[] currentDestinationMethodParameterTypes = currentDestinationMethod
.getParameterTypes();
Class[] currentSourceMethodParameterTypes = currentSourceMethod
.getParameterTypes();
if (currentDestinationMethodParameterTypes.length != 1
|| currentSourceMethodParameterTypes.length != 0) {
continue;
} // 源类方法返回类型必须和目标类方法参数类型一致
if (!currentDestinationMethodParameterTypes[0]
.equals(currentSourceMethod.getReturnType())) {
continue;
} // 赋值
try {
Object parameter = currentSourceMethod.invoke(source,
new Object[0]);
currentDestinationMethod.invoke(destination,
new Object[] { parameter });
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
EntityA a = new EntityA(1, "名称", 123F, "abc");
EntityB b = new EntityB();
BeanUtils.copyProperties(b, a);
System.out.println(b);
}
}class EntityA {
private int id; private String productName; private float price; private String bbb; public String getBbb() {
return bbb;
} public void setBbb(String bbb) {
this.bbb = bbb;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public float getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public EntityA() { } public EntityA(int id, String productName, float price, String bbb) {
this.id = id;
this.productName = productName;
this.price = price;
this.bbb = bbb;
}
}class EntityB {
private int id; private String productName; private double price; private String aaa; public String getAaa() {
return aaa;
} public void setAaa(String aaa) {
this.aaa = aaa;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public EntityB() {
} public EntityB(int id, String productName, double price, String aaa) {
this.id = id;
this.productName = productName;
this.price = price;
this.aaa = aaa;
} public String toString() {
return this.id + "," + this.productName + "," + this.price + ","
+ this.aaa;
}
}

解决方案 »

  1.   

    你的这个命题就很不完善(我猜测他目的是要考察你对java reflection机制的了解程度)首先,类对象在内存中是以代码形式存在的,其变量并不会分配实际的内存,只有在生成该类的对象时才具体给变量赋值,所以不知道如何类A字段的值依次赋值给类B,我觉得题目中的类改为对象将为合适。再次,如果两个类的字段名完全相等,但是它们都是私有的话,并假设它们没有公开的getter/setter方法,你就没有办法将类A字段的值依次赋值给类B,所以你只能将public的字段值传递过去,而题目中去并没有这么讲。
      

  2.   

    我觉得LZ的问题就是让做一个 struts 的 BeanUtils.copyProperties
    所以2楼的兄弟说得对,必须是实体类,必须有公开的getter/setter方法
      

  3.   

    恩是 这样子的, 我java的底子弱. 我想把一个pojo对象拷贝一下.返回,数据安全的需要.