利用反射得到Method对象,调用方法形参为 对象数组或可变参数与其他形参不同的地方

标题:利用反射得到Method对象,调用方法形参为 对象数组或可变参数与其他形参不同的地方

一、利用反射得到Method对象,调用方法
注意要点,若调用的方法中形参为 对象数组可变参数则会有不一样的地方

如下:
1)jdk1.4,1.5的不同

	JDK1.4 method.invoke(Object obj,Object[] args);
	JDK1.5 method.invoke(Object obj,Object... args);

2)情况一、对于方法为public void hhh(String name,String password){}

解决方案:

	jdk1.4 method.invoke(obj,new String[]{"aa","bb"});
	jdk1.5 method.invoke(obj,new String[]{"aa","bb"});
		或 method.invoke(obj,"aa","bb");

3)情况二、对于方法为public void hhh(String... args){} 或public void hhh(String[] args){}
解决方案:

jdk1.4解决方案 method.invoke(obj,new Object[]{new String[]{"aa","bb"}});
			或 method.invoke(obj,(Object)new String[]{"aa","bb"});
jdk1.5为了兼容1.4,故也需要这样写
/*
	JDK1.4 method.invoke(Object obj,Object[] args);
	JDK1.5 method.invoke(Object obj,Object... args);
	情况一,对于方法为public void hhh(String name,String password){}
	jdk1.4 method.invoke(obj,new String[]{"aa","bb"});
	jdk1.5 method.invoke(obj,new String[]{"aa","bb"});
		或 method.invoke(obj,"aa","bb");
	情况二,对于方法为public void hhh(String... args){}
			或public void hhh(String[] args){}
			jdk1.4解决方案 method.invoke(obj,new Object[]{new String[]{"aa","bb"}});
			或 method.invoke(obj,(Object)new String[]{"aa","bb"});
			jdk1.5为了兼容1.4,故也需要这样写
*/

部分测试代码如下:

//public void bbb(String name,String password) 获取Method对象
	@Test
	public void test07() throws Exception{
		//加载类
		Class<?> clazz = Class.forName("com.msb.test03.Person");
		
		//获取对象
		Constructor<?> constructor=clazz.getDeclaredConstructor();
		constructor.setAccessible(true);
		Object obj = constructor.newInstance();
		
		//public void bbb(String name,String password) 获取Method对象
		Method method = clazz.getMethod("bbb",String.class,String.class);
		method.invoke(obj,new String[] {"aa","bb"});
		//method.invoke(obj, "aa","bb");//或者这样写

	}

//public static void main(String[] args) 获取Method对象
@Test
public void test04() throws Exception{
	//加载类
	Class<?> clazz = Class.forName("com.msb.test03.Person");
	
	//获取对象
	Constructor<?> constructor=clazz.getDeclaredConstructor();
	constructor.setAccessible(true);
	Object obj = constructor.newInstance();
	
	//public static void main(String[] args) 获取Method对象
	Method method = clazz.getMethod("main",String[].class);
//		method.invoke(obj,(Object)new String[] {"aa","bb"});
	method.invoke(obj,new Object[] {new String[] {"aa","bb"}});

}

//public void aaa(Person... p) 获取Method对象
@Test
public void test06() throws Exception{
	//加载类
	Class<?> clazz = Class.forName("com.msb.test03.Person");
	
	//获取对象
	Constructor<?> constructor=clazz.getDeclaredConstructor();
	constructor.setAccessible(true);
	Object obj = constructor.newInstance();
	
	//public void aaa(Person... p) 获取Method对象
	Method method = clazz.getMethod("aaa",Person[].class);
//		method.invoke(obj,(Object)new Person[] {new Person(""),new Person("")});
	method.invoke(obj,new Object[] {new Person[] {new Person(""),new Person("")}});

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值