A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© Mr.Hao 中级黑马   /  2014-7-11 11:44  /  708 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

  13.                 } catch (ClassNotFoundException e) {
  14.                         e.printStackTrace();
  15.                 }
  16.                 new Outer().fun1(3);
  17.         }
  18. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马