访问方法
方法 | 说明 |
getName() | 获得该方法的名称 |
getParameterTypes() | 按照声明顺序以Class数组的形式获得该方法的各个参数的类型 |
getReturnType() | 以Class对象的形式获得该方法的返回值的类型 |
getExceptionTypes() | 以Class数组的形式获得该方法可能抛出异常的类型 |
invoke(Object obj,Object...args) | 利用指定参数args执行指定对象obj中的该方法,返回值为Object类型 |
isVarArgs() | 查看该构造方法是否允许带有可变数量的参数,如果允许则返回true,否则返回false |
getModifiers() | 获得可以解析出该方法所采用修饰符的整数 |
实例
首先创建一个Example_03类
public class Example_03 {static void staticMethod() {
System.out.println("执行staticMethod()方法");
}
public int publicMethod(int i) {
System.out.println("执行publicMethod()方法");
return i * 100;
}
protected int protectedMethod(String s, int i)
throws NumberFormatException {
System.out.println("执行protectedMethod()方法");
return Integer.valueOf(s) + i;
}
private String privateMethod(String... strings) {
System.out.println("执行privateMethod()方法");
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < strings.length; i++) {
stringBuffer.append(strings[i]);
}
return stringBuffer.toString();
}
}
package java反射;
import java.lang.reflect.Method;
/*
* 通过反射访问Example_类中的所有方法,将各个方法的名称,
* 入口参数类型,返回值类型等信息输出到控制台
*/
public class Main_03 {
public static void main(String[] args) {
Example_03 example = new Example_03();
Class exampleC = example.getClass();
// 获得所有方法
Method[] declaredMethods = exampleC.getDeclaredMethods();
for (int i = 0; i < declaredMethods.length; i++) {
Method method = declaredMethods[i]; // 遍历方法
System.out.println("名称为:" + method.getName()); // 获得方法名称
System.out.println("是否允许带有可变数量的参数:" + method.isVarArgs());
System.out.println("入口参数类型依次为:");
// 获得所有参数类型
Class[] parameterTypes = method.getParameterTypes();
for (int j = 0; j < parameterTypes.length; j++) {
System.out.println(" " + parameterTypes[j]);
}
// 获得方法返回值类型
System.out.println("返回值类型为:" + method.getReturnType());
System.out.println("可能抛出的异常类型有:");
// 获得方法可能抛出的所有异常类型
Class[] exceptionTypes = method.getExceptionTypes();
for (int j = 0; j < exceptionTypes.length; j++) {
System.out.println(" " + exceptionTypes[j]);
}
boolean isTurn = true;
while (isTurn) {
// 如果该方法的访问权限为private,则抛出异常,即不允许访问
try {
isTurn = false;
if ("staticMethod".equals(method.getName()))
method.invoke(example); // 执行没有入口参数的方法
else if ("publicMethod".equals(method.getName()))
System.out.println("返回值为:" + method.invoke(example, 168)); // 执行方法
else if ("protectedMethod".equals(method.getName()))
System.out.println("返回值为:" + method.invoke(example, "7", 5)); // 执行方法
else if ("privateMethod".equals(method.getName())) {
Object[] parameters = new Object[] { new String[] { "M", "W", "Q" } }; // 定义二维数组
System.out.println("返回值为:" + method.invoke(example, parameters));
}
} catch (Exception e) {
System.out.println("在执行方法时抛出异常," + "下面执行setAccessible()方法!");
method.setAccessible(true); // 设置为允许访问
isTurn = true;
}
}
System.out.println();
}
}
}
运行结果:
名称为:protectedMethod
是否允许带有可变数量的参数:false
入口参数类型依次为:
class java.lang.String
int
返回值类型为:int
可能抛出的异常类型有:
执行protected()方法
返回值为:12
名称为:publicMethod
是否允许带有可变数量的参数:false
入口参数类型依次为:
int
返回值类型为:int
可能抛出的异常类型有:
执行publicMethod()方法
返回值为:16800
名称为:staticMethod
是否允许带有可变数量的参数:false
入口参数类型依次为:
返回值类型为:void
可能抛出的异常类型有:
执行staticMethod()方法
名称为:privateMethod
是否允许带有可变数量的参数:true
入口参数类型依次为:
class [Ljava.lang.String;
返回值类型为:class java.lang.String
可能抛出的异常类型有:
在执行方法时抛出异常,下面执行setAccessible()方法!
执行privateMethod()方法
返回值为:MWQ