Java反射之方法

 @Test
    public void testInvoke() throws Exception {
        Class clazz = Main.class;
        Object object = clazz.newInstance();
        Field[] fields = clazz.getFields();
        Method[] methods = clazz.getMethods();
        Method method = clazz.getDeclaredMethod("sayHello", String.class, String.class);
        method.setAccessible(true);
        method.invoke(object,"324","345");
    }

对于私有方法method.setAccessible(true);这个不需要的…

你可能感兴趣的:(Java反射之方法)