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


    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);
    	}
    
    }
  • 相关阅读:
    转载的,讲解java.util的集合类
    LinkedList
    数据结构
    随笔
    spring boot初步尝试
    spring boot
    java8 新特性
    volatile 续
    java 线程基础学习
    java 基础
  • 原文地址:https://www.cnblogs.com/harbin1900/p/8480949.html
Copyright © 2020-2023  润新知