package t11;
import java.lang.reflect.Method;
class test{
private void testA(String arg){
System.out.println("private----"+arg);
}
public void testB(){
System.out.println("public");
}
protected void testC(){
System.out.println("protected");
}
void testD(){
System.out.println("default");
}
}
public class ReflectTest {
public static void main(String args[]) throws Exception{
Class<?> forName = Class.forName("t11.test");
//使用getMethod拿不到私有方法
Method[] mes = forName.getDeclaredMethods();
for(Method me : mes){
String methodName = me.getName();
System.out.println(methodName);
}
//必须使用getDeclaredMethod采用拿到私有方法
Method method1 = forName.getDeclaredMethod("testA",String.class);
//必须设置为true,否則出现异常
method1.setAccessible(true);
method1.invoke(forName.newInstance(),"invokeMethod--str");
}
}