java invoke 反射的使用方法

java invoke:动态调用类的方法
这个的例子,用在导入数据的时候,可以灵活的使用。很方便、实用。
1.User.java
view plain
package cvr.vo;

public class User {

private String username;

private String password;

private Integer value1,value2,value3,value4,value5,value6,value7,value8,value9,value10;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Integer getValue1() {
return value1;
}

public void setValue1(Integer value1) {
this.value1 = value1;
}

public Integer getValue10() {
return value10;
}

public void setValue10(Integer value10) {
this.value10 = value10;
}

public Integer getValue2() {
return value2;
}

public void setValue2(Integer value2) {
this.value2 = value2;
}

public Integer getValue3() {
return value3;
}

public void setValue3(Integer value3) {
this.value3 = value3;
}

public Integer getValue4() {
return value4;
}

public void setValue4(Integer value4) {
this.value4 = value4;
}

public Integer getValue5() {
return value5;
}

public void setValue5(Integer value5) {
this.value5 = value5;
}

public Integer getValue6() {
return value6;
}

public void setValue6(Integer value6) {
this.value6 = value6;
}

public Integer getValue7() {
return value7;
}

public void setValue7(Integer value7) {
this.value7 = value7;
}

public Integer getValue8() {
return value8;
}

public void setValue8(Integer value8) {
this.value8 = value8;
}

public Integer getValue9() {
return value9;
}

public void setValue9(Integer value9) {
this.value9 = value9;
}

}

2.Go.java
view plain
package cvr.go;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cvr.vo.User;

public class Go {

public static void main(String[] args) {
int [] vals = new int[]{1,2,3,4,5,6,7,8,9,10};
User user = Go.setUserValues(vals);
List list = Go.getUserValues(user);
for (int i = 0; i < list.size(); i++) {
int j = i+1;
System.out.println("value" + j + "=" + list.get(i));
}
}

public static User setUserValues(int [] vals) {
User user = new User();
try {
//获取Class的对象
Class c = user.getClass();
for (int i = 1; i <= 10; i++) {
//动态获取方法名
String methodName = "setValue"+i;
//设置方法的参数
Class[] args = new Class[1];
Integer u = new Integer(3);
args[0] = u.getClass();
Method method = c.getMethod(methodName, args);
//调用方法
method.invoke(user, vals[i-1]);
}
} catch (Exception e) {
e.printStackTrace();
}
return user;
}

public static List getUserValues(User user) {
List list = new ArrayList();
try {
Class c = user.getClass();
for (int i = 1; i <= 10; i++) {
String methodName = "getValue"+i;
Method method = c.getMethod(methodName, null);
Integer s = (Integer) method.invoke(user, null);
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值