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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 帅哥哥 高级黑马   /  2014-5-23 15:24  /  974 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 帅哥哥 于 2014-5-24 11:39 编辑

上代码:
  1. class Outer
  2. {
  3.         String s = "1";
  4.         void func_3()
  5.         {
  6.                 final String s = "2";
  7.                 class Inner_3
  8.                 {
  9.                         String s = "3";
  10.                         void func_2()
  11.                         {
  12.                                 String s = "4";
  13.                                 System.out.println(/*这里想访问final类型的那个s = "2",怎么办*/);
  14.                         }
  15.                 }
  16.         }
  17. }
复制代码



4 个回复

倒序浏览
  1. class Outer
  2. {
  3.         String s = "1";
  4.         void func_3()
  5.         {
  6.                 final String s = "2";
  7.                 final String sf = "2";
  8.                 class Inner_3
  9.                 {
  10.                         String s = "3";
  11.                         void func_2()
  12.                         {
  13.                                 String s = "4";
  14.                                 /*这里想访问final类型的那个s = "2",怎么办*/
  15.                                 System.out.println("Outer.this.s = "+Outer.this.s);
  16.                                 System.out.println("this.s = "+this.s);
  17.                                 System.out.println("s = "+s);
  18.                                 System.out.println("sf = "+sf);
  19.                         }
  20.                 }
  21.                 new Inner_3().func_2();
  22.         }
  23. }
  24. class Demo
  25. {
  26.         public static void main(String[] args)
  27.         {
  28.                 new Outer().func_3();
  29.         }
  30. }
  31. /*以下是执行结果,每一个引用都有自己所对应的变量,若想访问s = "2"就只能更换变量的名字了
  32. *Outer.this.s = 1
  33. *this.s = 3
  34. *s = 4
  35. *sf = 2
  36. */
复制代码

评分

参与人数 1技术分 +1 收起 理由
Silent_memory + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 帅哥哥 于 2014-5-23 16:54 编辑

不能修改源代码,发了这帖子之后我就后悔了,我好像想到了一个很简单的方法,还没试,等试试能的话再发出来~~~

哎呀,居然不对。。。。。
回复 使用道具 举报
看不懂啊
回复 使用道具 举报

就是定义了四个同名变量
一个在外部类成员位置上
一个在外部类成员函数内
一个在局部内部类成员位置上
一个在局部内部类成员函数内
问在局部内部类成员函数中怎么访问外部类成员函数中的局部变量
第一个,Outer.this可以访问
第二个,不知道
第三个,this可以访问
第四个,直接就可以访问
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马