//实体类对象
package com;
public class Persion {}
测试方法及工具类:
package com;
import java.lang.reflect.Method;
public class mainTest {
//setter方法注入
private static void setter(Object obj, String att, Object value, Class> type) {
try {
StringBuilder sb = new StringBuilder();
sb.append("set").append(att.substring(0, 1).toUpperCase()).append(att.substring(1));
Method met = obj.getClass().getMethod(sb.toString(), type);
met.invoke(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
//getter方法注入
private static Object getter(Object obj, String att) {
Object temp=null;
try{
StringBuilder sb = new StringBuilder();
sb.append("get").append(att.substring(0, 1).toUpperCase()).append(att.substring(1));
Method met = obj.getClass().getMethod(sb.toString());
temp = met.invoke(obj);
}catch(Exception e){
e.printStackTrace();
}
return temp;
}
public static void main(String[] args) {
Persion per = new Persion();
System.out.println("setter开始。。。。。");
for(int i=1;i<8;i++){
setter(per, "name"+i, "name"+i, String.class);
}
System.out.println("setter结束。。。。。");
System.out.println("getter开始。。。。。");
for(int i=1;i<8;i++){
Object obj= getter(per, "name"+i);
System.out.println(obj);
}
System.out.println("getter结束。。。。。");
}
}