1、调用静态方法

Class threadClazz = Class.forName("java.lang.Math");
Method method = threadClazz.getMethod("abs", long.class);
System.out.println(method.invoke(null, -10000l));

只需要将invoke方法的第一个参数设为null即可。

2、调用类中构造方法

取得类中指定参数类型的构造

public Constructor getConstructor(Class… parameterTypes) throws NoSuchMethodException, SecurityException

只能取得类中public权限的构造方法

public Constructor getDeclaredConstructor(Class… parameterTypes)

可以取得类中全部构造方法,包含私有构造。

3、调用类中指定名称的普通方法

public Method getMethod(String name, Class… parameterTypes)
//方法有重载所以要传名称和参数类型取得本类以及父类中所有public方法
public Method getDeclaredMethod(String name, Class                    
本文转载于:https://www.yisu.com/zixun/581627.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。