泛型与反射的使用
作者:互联网
package com.yonyou.cyxdms.retail.service.service.manufacturerorders;
import com.yonyou.cyxdms.retail.service.entity.vo.salesorders.manufactuer.Test001;
import com.yonyou.cyxdms.retail.service.entity.vo.salesorders.manufactuer.Test002;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Test001 test001 = new Test001();
test001.setA("10086");
test001.setB("10010");
Test002 test002 = new Test002();
getOO(test002,test001);
System.out.println(test002.getA() + "---" + test002.getB());
}
private static void getOO(Object object,Object dateVO) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class<?> aClass = object.getClass();
Class<?> dataClass = dateVO.getClass();
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method : declaredMethods) {
// set
if (method.getName().startsWith("set")){
Method method1 = dataClass.getMethod("get" + method.getName().substring(3));
Object invoke = method1.invoke(dateVO);
method.invoke(object, invoke);
}
}
}
}
返回值
10086---10010
标签:反射,test001,service,test002,method,使用,泛型,import,Method 来源: https://www.cnblogs.com/jabez1992/p/13187870.html