利用反射获取方法参数名--从Parameter类型转换到String类型参数名

反射获取的Parameter类型参数名转换为String类型参数名

  1. 直接输出获取的Parameter类型参数名。
public class Hello {
	public String print(String str,int i) {
		return str+i;
	}
	//输出Parameter类型方法参数名
	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		Hello hello = new Hello();
		Class helloClass = hello.getClass();
		Method method = helloClass.getMethod("print",String.class,int.class);
		Parameter[] param =  method.getParameters();
		for(int i=0;i<param.length;i++) {
			System.out.println(param[i]);
		}
	}

}

输出结果为:

java.lang.String str
int i
  1. Parameter类型方法参数名转换为String类型。
public class Hello {
	public String print(String str,int i) {
		return str+i;
	}
	//获取print方法参数名称,将Parameter类型转换到String类型参数名
	public String[] getParametersName(Parameter[] param) {
		String[] paramname = new String[param.length];
		for(int i=0;i<param.length;i++) {
			String param1 = param[i].toString();
			String name = param1.substring(param1.indexOf(" ")+1);
			paramname[i] = name;
			System.out.println(name);
		}
		return paramname;
	}
	//输出参数
	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		Hello hello = new Hello();
		Class helloClass = hello.getClass();
		Method method = helloClass.getMethod("print",String.class,int.class);
		Parameter[] param =  method.getParameters();
		hello.getParametersName(param);
	}

}

输出结果为:

str
i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值