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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lfs454766767 中级黑马   /  2014-8-24 15:05  /  1093 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 lfs454766767 于 2014-8-26 08:43 编辑
  1. class OuterX {
  2.         int x = 10;
  3.         
  4.         void method() {
  5.                 final int x = 100;
  6.                 class Inner {
  7.                          int x = 1000;
  8.                          void method() {
  9.                                 int x =10000;
  10.                                 System.out.println(x);
  11. System.out.println(this.x);
  12.                                 System.out.println(OuterX.this.x);
  13.                                 
  14.                          }
  15.                 }
  16.                 new Inner().method();
  17.         }
  18. }
  19. public class O{
  20.         public static void main(String[] args) {
  21.                 OuterX x = new OuterX();
  22.                 x.method();
  23.         }
  24. }
复制代码
怎么在内部类中的方法内访问 final的那个 x??

7 个回复

倒序浏览
this.x就是
回复 使用道具 举报

this.x调用的是1000的那个啊
回复 使用道具 举报
只能改名字.int x = 3; { int x = 4; }你这种, 你想在块内都能访问两个x是无法做到的.
回复 使用道具 举报
编译过没?
回复 使用道具 举报

我也觉得只能改名字: Inner中的X和 method中的x重名:根据作用域原理  Inner中的x会会覆盖method中的x
但Outer类中的x可以根据Outer.this.x来与Inner中的区别
回复 使用道具 举报
OuterX.this.x
加上外部类的名字
回复 使用道具 举报
faith 中级黑马 2014-8-25 15:49:03
8#
要把methood这个方法名带入
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马