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;
}
}
分享到:
相关推荐
本程序实现Invoke反射,内置说明txt文档,可以参考
Java反射 调用空参方法 调用Object类型参数的方法 调用基本类型参数的方法 调用基本类型数组参数的方法 调用String数组参数的方法 调用Object数组参数的方法 调用私有方法
java反射机制描述及Method.invoke解释
主要介绍了Java反射机制及Method.invoke详解,本文讲解了JAVA反射机制、得到某个对象的属性、得到某个类的静态属性、执行某对象的方法、执行某个类的静态方法等内容,需要的朋友可以参考下
Method,invoke,getMethod等反射私有方法和私有属性
Chp15 反射Key Point ●Class 对象及其基本操作●Method 对象以及invoke 方法●标注练习 1. (类对象)要获得类对象,有三种不同的方式,分别为_____________________、___________________、_____________________...
Java反射反射机制,通过反射,在不修改源代码的情况下实现对类的操作。JAVA反射机制是在运行状态中...这种动态获取信息以及动态调用对象方法(**方法名.invoke(类对象),类似于p.eat()**)的功能称为java语言的反射机制。
java反射机制详解及Method.invoke解释.pdf
在使用反射可以得到类模板class对象,那么得到类模板的class对象后,如何调用某个已知的且需要的方法呢?答案是使用method类的invoke方法,那么实现的代码如下,结合jdk的api和该代码便可以知道原理 package ...
java reflect Method getDeclaredMethod invoke getConstructor
Java提供了一套机制来动态执行方法和构造方法,以及数组操作等,这套机制就叫——反射。反射机制是如今很多流行框架的实现基础,其中包括Spring、Hibernate等。原理性的问题不是本文的重点,接下来让我们在实例中...
java动态代理 public class HireProxy implements InvocationHandler { //被代理的真实角色 private Object obj; public HireProxy(Object obj) { super(); this.obj = obj; } //第二个参数method,被...
这样你就可以使用 Constructor 创建新的对象,用 get() 和 set() 方法读取和修改与 Field 对象关联的字段,用invoke() 方法调用与 Method 对象关联的方法。 另外,还可以调用 getFields() getMethods
java vm很清楚你编写的class的结构,所以j2sdk提供了反射机制来动态的调用class的方法,举个例子,我们在调用对象的方法,常用的可能是obj.invokedmethod,而用发射机制,可以写成Invoker.invoke("className",...
DumpMethods.java 使用反射机制来获取类中的方法 getClassName.java 利用反射机制获取类的名字 invokeConstructor.java 利用反射机制调用构造器 invokeMethod.java 利用反射机制调用成员方法 listConstructors....
jOOR 在java原生的反射基础上进行了简单的封装,使得反射使用方便不少! 举个简单的列子,供大家参考。 java 原生反射写法: try { Method m1 = department.getClass().getMethod("getEmployees"); Employee ...
主要介绍了InvocationHandler中invoke()方法的调用问题分析,具有一定参考价值,需要的朋友可以了解下。
1. 获取Class类对象的方法 /* * Class Class.forName(String packageNameAndClassName); * Class 类的静态成员方法通过完整的包名.类名获取对应的.class文件的class对象 * 同时也可以作为.class文件加载的方式 *...
整理的一个java反射的使用框架 可以适用各种上层class的调用包括其method的调用 内部类及接口那些没写 你可以自己基于我这个框架的方式再结合资料自己编写 使用非常简单 效果非常强大
通配符定义的变量主要用作引用,调用与参数化无关的方法,如果要调用与参数化相关的方法,那么必须在使用?通配符引用之前调用,否则就与java 5提供泛型的目的背道而驰了。 b..向下限定通配符: 正确:Vector...