本帖最后由 Mr.Hao 于 2014-7-11 11:51 编辑
- class Outer
- {
- private int x = 1;
- private String s = "dwdw";
- public char c = 'c';
- public void fun1(final int temp){
- final long y = 2;
- class Inner{
- public void fun2(){
- System.out.println(temp);
- System.out.println(x);
- System.out.println(y);
- System.out.println(s);
- System.out.println(c);
- }
- }
- new Inner().fun2();
- }
- }
复制代码- </blockquote></div><div class="blockcode"><blockquote>public class Test3
- {, InstantiationException, IllegalAccessException, InvocationTargetException
- {
- try {
- Class<?> s = Class.forName("com.itheima.Outer$1Inner");//反射内部类
- Constructor[] cons = s.getDeclaredConstructors(); //得到全部构造方法
- for(Constructor cc : cons)
- System.out.println(cc);
- System.out.println(cons[0].isAccessible()); //唯一的构造方法为private类型
-
- cons[0].newInstance(new Outer(), 20); //通过构造方法实例化一个局部内部对象
- //问题就是上面的实例化对象到底用什么去接收,如何能够调用其中的fun2
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- new Outer().fun1(3);
- }
- }
复制代码
|
|