public Object invoke(Object obj,
Object... args)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException

对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。个别参数被自动解包,以便与基本形参相匹配,基本参数和引用参数都随需服从方法调用转换。

如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。

如果底层方法所需的形参数为 0,则所提供的 args 数组长度可以为 0 或 null。

如果底层方法是静态的,并且尚未初始化声明此方法的类,则会将其初始化。

如果方法正常完成,则将该方法返回的值返回给调用者;如果该值为基本类型,则首先适当地将其包装在对象中。但是,如果该值的类型为一组基本类型,则数组元素 被包装在对象中;换句话说,将返回基本类型的数组。如果底层方法返回类型为 void,则该调用返回 null。

obj - 从中调用底层方法的对象(简单的说就是调用谁的方法用谁的对象)
args - 用于方法调用的参数 
     package test922;  

     public class InvokeObj {  

         public void show() {
System.out.println("无参show()方法。");
}
public void show (String name) {
System.out.println("show方法:" + name);
}
public String[] arrayShow (String[] arr) {
return arr;
}
public String StringShow (String str) {
return str;
}
public int intShow (int num) {
return num;
}
}
     package test922;  

     import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; public class MethodInvokeTest {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<InvokeObj> clazz = InvokeObj.class;
Method[] methods = clazz.getMethods();
System.out.println("以下输出InvokeObj类的方法:");
for (Method method : methods) {
System.out.println(method);
}
System.out.println("InvokeObj类的无参show()方法:");
Method method1 = clazz.getMethod("show", null);
//会执行无参show()方法
Object obj = method1.invoke(new InvokeObj(),null);
System.out.println("输出无参show()方法的返回值:"+obj);
System.out.println("InvokeObj类的show()方法: ");
Method method2 = clazz.getMethod("show", String.class);
Object obj1 = method2.invoke(new InvokeObj(), "hello,world");
System.out.println("输出有参show()方法: ");
System.out.println("InvokeObj类的arrayShow()方法: ");
Method method3 = clazz.getMethod("arrayShow", String[].class);
String[] strs = new String[]{"hello", "world", "!"};
//数组类型的参数必须包含在new Object[]{}中,否则会报IllegalArgumentException
String[] strings = (String[]) method3.invoke(new InvokeObj(), new Object[]{strs});
for (String str : strings) {
System.out.println("arrayShow的数组元素:" + str);
}
System.out.println("InvokeObj类的StringShow()方法: ");
Method method4 = clazz.getMethod("StringShow", String.class);
String string = (String) method4.invoke(new InvokeObj(), "Thinking in java");
System.out.println("StringShow()方法的返回值: " + string);
System.out.println("InvokeObj类的intShow()方法: ");
Method method5 = clazz.getMethod("intShow", int.class);
int num = (int) method5.invoke(new InvokeObj(), 89);
System.out.println("intShow()方法的返回值: " + num);
}
}
     以下输出InvokeObj类的方法:
public void test922.InvokeObj.show(java.lang.String)
public void test922.InvokeObj.show()
public java.lang.String[] test922.InvokeObj.arrayShow(java.lang.String[])
public java.lang.String test922.InvokeObj.StringShow(java.lang.String)
public int test922.InvokeObj.intShow(int)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
InvokeObj类的无参show()方法:
无参show()方法。
输出无参show()方法的返回值:null
InvokeObj类的show()方法:
show方法:hello,world
输出有参show()方法:
InvokeObj类的arrayShow()方法:
arrayShow的数组元素:hello
arrayShow的数组元素:world
arrayShow的数组元素:!
InvokeObj类的StringShow()方法:
StringShow()方法的返回值: Thinking in java
InvokeObj类的intShow()方法:
intShow()方法的返回值: 89

method.invoke()使用的更多相关文章

  1. java反射机制详解 及 Method.invoke解释

    JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为ja ...

  2. java反射机制 + Method.invoke解释 getMethod + 反射理解

    功能: 通过读取另一个Dll去创建一个控件(Form,Button,TextBox,DataGridView),然后对当中一些属性进行检查. 创建控件的大致流程是,Assembly->Modul ...

  3. (转)Java.lang.reflect.Method invoke方法 实例

    背景:今天在项目中用到Method 的invoke方法,但是并不理解,查完才知道,原来如此! import java.lang.reflect.Method; /** * Java.lang.refl ...

  4. method.invoke()s

    在框架中经常会会用到method.invoke()方法,用来执行某个的对象的目标方法.以前写代码用到反射时,总是获取先获取Method,然后传入对应的Class实例对象执行方法.然而前段时间研究inv ...

  5. method.invoke(...)反射点

    import java.lang.reflect.Method; import java.util.Arrays; /** * @Author: hoobey * @Description: * @D ...

  6. Java反射机制及Method.invoke详解

    JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为ja ...

  7. 【java】method.invoke(方法底层所属对象/null,new Object[]{实际参数})

    反射调方法时无论是静态/非静态,固定/可变参数,都有Object对象数组对参数进行包装. package com.tn.clas; import java.lang.reflect.Method; i ...

  8. java中Method.invoke方法参数解析

    通过发射的机制,可以通过invoke方法来调用类的函数.invoke函数的第一个参数是调用该方法的实例,如果该方法是静态方法,那么可以用null或者用类来代替,第二个参数是变长的,是调用该方法的参数. ...

  9. Java中Method.invoke方法,反射?

    正常来说,我们调用对象的方法是通过dot运算符来进行的,这里我们介绍另一种方法,有以下几个步骤:1,获取该类的Class Type:2,通过getMethod方法获取Method对象:3,通过调用in ...

随机推荐

  1. ZZULIoj 1908 小火山的围棋梦想

    Description   小火山最近喜欢上了围棋.   对于围棋,其实小火山是一窍不通的.现在棋盘上,有很多小火山的棋子. 如果棋盘上有这样的一个位置, 那么这个位置也会变成小火山 的棋子:这样的位 ...

  2. bzoj2631 tree LCT 区间修改,求和

    tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 4962  Solved: 1697[Submit][Status][Discuss] Des ...

  3. visual studio用"查找替换"来删掉源代码中所有//方式的纯注释和空行

    visual studio用"查找替换"来删掉源代码中所有//方式的纯注释和空行 注意:包括/// <summary>这样的XML注释也都删掉了. 步骤1/2(删除注释 ...

  4. BZOJ3126: [Usaco2013 Open]Photo

    n<=200000个点,m<=100000个区间,每个区间有且仅有一个点,求最多几个点,无解-1. http://www.cnblogs.com/Chorolop/p/7570191.ht ...

  5. BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名

    n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...

  6. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  7. Java实验--统计字母出现频率及其单词个数

    本周的实验要求在之前实现统计单词的基础之上(可以见之前博客的统计单词的那个实验),对其进行修改成所需要的格式,统计字母出现频率的功能,并按照一定的格式把最终结果的用特定的格式在文本中显示出来 统计过程 ...

  8. 转:Linux性能评测工具之一:gprof篇

    1 简介 改进应用程序的性能是一项非常耗时耗力的工作,但是究竟程序中是哪些函数消耗掉了大部分执行时间,这通常都不是非常明显的.GNU 编译器工具包所提供了一种剖析工具 GNU profiler(gpr ...

  9. easyui combotree选项重复

    现象 编辑,赋值出现重复选项 原因 值之间有空格,比如我取值是3, 4, 6要改成3,4,6 注意:数值之间的空格去掉了

  10. 【Nginx】发送响应

    请求处理完毕后,需要向用户发送http响应,告知客户端Nginx的执行结果.http响应主要包括响应行.响应头部.包体三部分.发送http响应时需要执行发送http头部(发送http头部时也会发送响应 ...