`
mywhy
  • 浏览: 63908 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

利用反射实现动态方法调用

 
阅读更多
package test.my.chap0302;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodTest {

	/**
	 * @param args
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		
		//利用反射实现动态方法调用
		Method sin = Math.class.getDeclaredMethod("sin",Double.TYPE);
		System.out.println(sin);
		Double d = (Double)sin.invoke(null,new Integer(2));
		System.out.println(d);
		Method str = String.class.getDeclaredMethod("equals",Object.class);
		Boolean bl = (Boolean) str.invoke(new String("利用反射实现动态调用方法"),"利用反射实现动态调用方法");
		System.out.println(bl);
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics