黑马程序员技术交流社区

标题: 内部类 [打印本页]

作者: lingxia125    时间: 2015-8-5 09:24
标题: 内部类
本帖最后由 lingxia125 于 2015-9-11 17:42 编辑
  1. class Outer
  2. {
  3.         
  4.         void method()
  5.         {
  6.                  int y=4;
  7.                 class Inner
  8.                 {
  9.                         void function()
  10.                         {
  11.                                 System.out.println(y);
  12.                         }
  13.                 }
  14.                 new Inner().function();
  15.         }
  16. }
  17. class  InnerClassDemo2
  18. {
  19.         public static void main(String[] args)
  20.         {
  21.                 Outer ou=new Outer();
  22.                 ou.method();
  23.         }
  24. }
复制代码
内部类在局部时不是不能访问局部的变量吗?为什么我局部中y没有用final修饰,还是可以正常运行,得出结果y=4

作者: WENBO233    时间: 2015-8-5 14:32
内部类可以直接访问外部类的成员,包括私有,外部类要访问内部类的成员,必须创建对象。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2