反射获取成员方法并使用

反射获取成员方法并使用



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

public class demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> c = Class.forName("javase.day22.demo01.Student");
        Method[] methods = c.getDeclaredMethods();
        //Method[] methods = c.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("----------");
        Method m = c.getMethod("method1");
        //获取无参构造方法创建对象
        Constructor<?> con = c.getConstructor();
        Object obj = con.newInstance();
        m.invoke(obj);



    }
}

上一篇:golang查看数据类型


下一篇:Js中Reflect对象