本帖最后由 李兴 于 2014-1-4 18:12 编辑
通过反射得到内部类的私有方法,编译失败,求解- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- class DemoA
- {
- public static void main(String[] args) throws Exception
- {
- Class classB = DemoA.DemoB.class;
- Constructor constructor = classB.getDeclaredConstructor(null);//这行报错
- DemoB db = (DemoB) constructor.newInstance(null);
- Method method = classB.getDeclaredMethod("sop", null);
- method.setAccessible(true);
- method.invoke(db, null);
- }
-
- class DemoB
- {
- private DemoB(){}
- private void sop(){
- System.out.println("DemoB");
- }
- }
- }
复制代码
该怎样改
|