1.6 理解 Method 对象
java.lang.reflect.Method 是表示方法的元数据的类,Method 类支持的方法如下表所示:
| 方法 | 说明 |
|---|---|
| Class getDeclaringClass() | 返回声明该方法的类的 Class 对象 |
| Class[] getExceptionTypes() | 返回该方法声明抛出的 异常 的 Class 对象数组 |
| int getModifiers() | 返回该方法的限定词,以 int 进行编码 |
| String getName() | 返回方法名 |
| Class[] getParameterTypes() | 返回参数的 Class 数组,按照声明顺序 |
| Class getReturnType() | 获取返回值的 Class 对象 |
| Object invoke(Object obj, Object[] args) | 在 obj 上使用 args 作为参数调用方法 |
每个 Method 对象都提供了对应方法的名字、参数类型、返回值和异常等信息,还可以通过 Method 对象调用方法。