标签:java 反射
Java 通过反射出方法名来运行 invoke该方法,此名称字符串一定完全匹配并且区分大小写
Method meth =cla.getMethod("setStr1", new Class[]{java.lang.String.class}); new class 参数类型集合
Object result=meth.invoke(object, new Object[]{"test..."}); new object 为输入参数类型的值 与上面的数目是对应的。
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Iterator; public class TestMe { private String str1; private String str2; private String str3; public void method1() { System.out.println("TestMe--------method1方法.."); } public void method2() { System.out.println("TestMe--------method2方法.."); } public void method3() { System.out.println("TestMe--------method3方法.."); } public void setStr1(String str1) { this.str1 = str1; System.out.println(str1); } public String getStr1() { return str1; } public void setStr2(String str2) { this.str2 = str2; } public String getStr2() { return str2; } public void setStr3(String str3) { this.str3 = str3; } public String getStr3() { return str3; } public static void main(String[] args) { test(); } private static void test() { Class cla = TestMe.class; try { Object object = cla.newInstance(); Method[] methods = cla.getMethods(); Method method; for (int i = 0; i < methods.length; i++) { method = methods[i]; System.out.println(method.getName()); // 方法的返回值类型 Type returntype = method.getReturnType(); System.out.println("方法返回值类型" + returntype); // 方法的参数类型 Type[] types = method.getGenericParameterTypes(); for (int j = 0; j < types.length; j++) { Type type = types[j]; System.out.println("方法参数类型type: " + j + "----" + type); } } Field[] fileds = cla.getDeclaredFields(); Field field; for (int i = 0; i < fileds.length; i++) { field = fileds[i]; System.out.println("---------------------------"); System.out.println(field.getType()); System.out.println(field.getName()); } Method meth =cla.getMethod("setStr1", new Class[]{java.lang.String.class}); Object result=meth.invoke(object, new Object[]{"test..."}); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果:
main
方法返回值类型void
方法参数类型type: 0----class [Ljava.lang.String;
method1
方法返回值类型void
method2
方法返回值类型void
method3
方法返回值类型void
setStr1
方法返回值类型void
方法参数类型type: 0----class java.lang.String
getStr1
方法返回值类型class java.lang.String
setStr2
方法返回值类型void
方法参数类型type: 0----class java.lang.String
getStr2
方法返回值类型class java.lang.String
setStr3
方法返回值类型void
方法参数类型type: 0----class java.lang.String
getStr3
方法返回值类型class java.lang.String
wait
方法返回值类型void
wait
方法返回值类型void
方法参数类型type: 0----long
方法参数类型type: 1----int
wait
方法返回值类型void
方法参数类型type: 0----long
hashCode
方法返回值类型int
getClass
方法返回值类型class java.lang.Class
equals
方法返回值类型boolean
方法参数类型type: 0----class java.lang.Object
toString
方法返回值类型class java.lang.String
notify
方法返回值类型void
notifyAll
方法返回值类型void
---------------------------
class java.lang.String
str1
---------------------------
class java.lang.String
str2
---------------------------
class java.lang.String
str3
test...
标签:java 反射
原文地址:http://yjm199.blog.51cto.com/4408395/1551592