- package com.heima.demo;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- public class Demo {
- public static void main(String[] args) throws Exception {
- Class<A> c = A.class;
- Constructor<A> con = c.getDeclaredConstructor();
- A a = con.newInstance();
-
- Method method = c.getDeclaredMethod("show");
- method.setAccessible(true); //暴力反射获得私有方法访问权限
- method.invoke(a);
- }
- }
- class A {
- private void show() {
- System.out.println("小强 你不要死啊!!!!!!!!!!!!!!!!");
- }
- }
复制代码 |