Java反射-访问方法

在这里插入图片描述

Method成员方法类

  • getModiifiers() 成员方法的修饰符
  • getName() 成员方法的名字
  • getType() 成员方法的声明类型
  • getParameterTypes() 成员方法的参数数组

获取成员方法

  • class.getMethods() 获取所有公有成员方法
  • class.getMethod(String name.Class<?>…paramterTypes) 获取指定的公有成员方法
  • class.getDeclaredMethods() 获取所有成员方法
  • class.getDeclaredMethods(String name.Class<?>…paramterTypes) 获取指定的成员方法

package zhao;

public class Example {
	static void ataticMethod() {
		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 zhao;

import java.lang.reflect.*;

public class Demo {
	public static void main(String[] args) {
		Example example = new Example();
		Class c = example.getClass();
		// 获得所有方法
		Method[] declaredMethods = c.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 isTrue = true;
			while (isTrue) {
				// 如果该方法的访问权限为private,则抛出异常,即不允许访问
				try {
					isTrue = 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);
					isTrue = true;
				}
			}
			System.out.println();
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值