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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王亚男 黑马帝   /  2011-12-22 17:12  /  1857 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 qwert 于 2011-12-23 18:25 编辑

各位前辈,我在看内部类的时候看到这里有个小小的疑问,就是下面这段代码中在不注释掉值为11和12的x的前提下,怎么访问到值为10的常量?
  1. abstract class AbsDemo
  2. {
  3.         abstract void show();
  4. }
  5. class Outer
  6. {
  7.         private int x=9;
  8.                
  9.         public void method()
  10.         {
  11.                   final int x = 10 ; //就是想访问这个x

  12.                   new AbsDemo()
  13.                   {
  14.                 int x = 11;
  15.                 void show()
  16.                 {
  17.                           int x = 12;
  18.                           System.out.println("x1="+x);
  19.                           System.out.println("x2="+this.x);
  20.                           System.out.println("x3="  );//就是想知道在不注释掉12和11这俩x的前提下,这写什么是能输出身为常量的那个x?
  21.                           System.out.println("x4="+Outer.this.x);
  22.                     }
  23.                 }.show();
  24.         }
  25. }

  26. class InnerClassDemo4
  27. {
  28.         public static void main(String[] args)
  29.         {
  30.                 new Outer().method();
  31.         }
  32. }
复制代码

2 个回复

倒序浏览
这种情况是实现不了的,
解决方案:1,改参数名
          2,传参
回复 使用道具 举报
马德强 发表于 2011-12-22 19:05
这种情况是实现不了的,
解决方案:1,改参数名
          2,传参

谢谢前辈,我也是想的这两种方法,只是想可能还有不知道的地方。
谢谢!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马