已经一个object中有一个名为getXXX的函数,如何执行这个函数?
应该用java的反射机制能解决,不知具体如何实现?
应该用java的反射机制能解决,不知具体如何实现?
解决方案 »
- 线程问题,线程已经取消了,也返回取消成功了,却还在继续运行,求救啊..:)
- 关于菱形打印
- 关于退出JFrame窗口是询问是否要退出的问题
- 关于在WEBSPHERE上发布的读取XML文件的问题
- vbscript与java的问题
- 郁闷啊,怎么能这样呢
- java API中有一个indexOf(String str, int fromIndex) 函数,其中的int参数是做什么用的?
- 应用程序全屏的问题,请大家进来看看!
- 有谁能详细解释一下RMI,可不可以这么认为,RMI就是包装简化了的socket?多谢了
- 哪里可下载JavaScript的参考书?
- 请各位帮帮忙 -- 如何获取 Java 运行行数
- Java中文件系统的路径问题(很着急!!!)
classe.getMethods()[0].invoke(...)
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class BeanTest{ public static void main(String[] args) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException { Haha haha = new Haha();
BeanInfo bean = Introspector.getBeanInfo(haha.getClass());
PropertyDescriptor[] properties = bean.getPropertyDescriptors();
for(PropertyDescriptor property : properties) {
// 获得属性的set方法
Method methodWrite = property.getWriteMethod();
// 获得属性的get方法
Method methodRead = property.getReadMethod(); if(methodWrite == null || methodRead == null) {
continue;
} methodWrite.invoke(haha, "abc"); Object obj = methodRead.invoke(haha);
System.out.println(property.getDisplayName() + " --> " + (String) obj);
}
}
}class Haha {
private String str1;
private String str2;
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
}
来找到这个属性的get方法后再行调用。import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class BeanTest { public static void main(String[] args) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
Haha haha = new Haha();
Class clazz = haha.getClass();
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields) {
PropertyDescriptor property = new PropertyDescriptor(field.getName(), clazz);
// 获得属性的set方法
Method methodWrite = property.getWriteMethod();
// 获得属性的get方法
Method methodRead = property.getReadMethod(); methodWrite.invoke(haha, "abc"); Object obj = methodRead.invoke(haha);
System.out.println(property.getDisplayName() + " --> " + (String) obj);
}
}
}
import java.lang.reflect.Method;public class TimeZone { public static void main(String[] args) throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
Haha haha = new Haha();
Method method = haha.getClass().getMethod("getName", new Class[] { String.class, String.class });
String result = (String) method.invoke(haha, new Object[] { "Bill", "Gates" });
System.out.println(result);
}
}class Haha {
public String getName(String firstname, String lastname) {
return firstname + " " + lastname;
}
}