import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
public class testInvoke {
public void work(){
System.out.println("-----------");
}
public testInvoke work(String a,Integer b){
System.out.println(a + b);
return this;
}
public void work(Integer b, int c ){
System.out.println(b + c);
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Class<?> clazz = testInvoke.class;
//Class<?> clazz = Class.forName("invoke.testInvoke");
//testInvoke tinvoke = new testInvoke(); Class<?> clazz = tinvoke.getClass();
System.out.println(clazz);
//如果源类的方法没有参数,则要用new Class[]{}
Method method1 = clazz.getMethod("work", new Class[]{});
Method method2 = clazz.getMethod("work", new Class[]{String.class, Integer.class});
Method method3 = clazz.getMethod("work", new Class[]{Integer.class, int.class});
Object invokeTest = clazz.newInstance();
/*
* Method类的invoke(Object obj,Object args[])方法接收的参数必须为对象,<br/>
* 如果参数为基本类型数据,必须转换为相应的包装类型的对象。invoke()方法的返回值总是对象,<br/>
* 如果实际被调用的方法的返回类型是基本类型数据,那么invoke()方法会把它转换为相应的包装类型的对象,再将其返回<br/>
*/
//invoke方法的第一个参数是源类的实例,第二个参数是实例的值
Object result1 = method1.invoke(invokeTest, new Object[]{});
Object result2 = method2.invoke(invokeTest, new Object[]{"aaaa",new Integer(10)});
Object result3 = method3.invoke(invokeTest, new Object[]{3,new Integer(4)});
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
Method[] methods = clazz.getMethods();
for(Method method : methods){
System.out.println(method.getName());
}
Field[] fileds = clazz.getFields();
for(Field filed: fileds){
System.out.println(filed.getName());
}
}
}
class invoke.testInvoke ----------- aaaa10 7 null invoke.testInvoke@de6ced null work [Ljava.lang.Class;@c17164 work [Ljava.lang.Class;@1fb8ee3 work [Ljava.lang.Class;@61de33 main [Ljava.lang.Class;@14318bb wait [Ljava.lang.Class;@ca0b6 wait [Ljava.lang.Class;@10b30a7 wait [Ljava.lang.Class;@1a758cb equals [Ljava.lang.Class;@1b67f74 toString [Ljava.lang.Class;@69b332 hashCode [Ljava.lang.Class;@173a10f getClass [Ljava.lang.Class;@530daa notify [Ljava.lang.Class;@a62fc3 notifyAll [Ljava.lang.Class;@89ae9e
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有