5

来源:互联网 发布:淘宝运营 编辑:程序博客网 时间:2024/04/27 20:39
package com.haizhitao.reflect;import java.lang.reflect.Method;public class TestPrivate{public static void main(String[] args) throws Exception{Private p = new Private();Class<?> classType = p.getClass();//getDeclaredMethod与getMethod方法不同,getDeclaredMethod返回所有声明的变量,        //getMethod只返回public变量.Method method = classType.getDeclaredMethod("sayHello", new Class[]{String.class});method.setAccessible(true);//压制Java中的访问控制检查Object result = method.invoke(p, new Object[]{"zhangsan"});System.out.println(result);}}//output://hello: zhangsan