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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 、海 中级黑马   /  2014-6-4 21:58  /  1155 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 、海 于 2014-6-4 22:02 编辑
  1. Outer {
  2.         int x =3;
  3.         void method(){
  4.                 class Inner{
  5.                         void function(){
  6.                                 System.out.println(Outer.this.x)//此处用法与直接用  x    有什么区别?
  7.                         }               
  8.                 }
  9.                 new Inner().function();
  10.         }

  11. }
复制代码


public class InnerClassTest {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                new Outer().method();
        }

}


7 个回复

倒序浏览
和直接使用x没有区别 直接使用x隐含了Outer.this.   就像类不写构造方法一样有默认的构造方法
回复 使用道具 举报
class outer
{
        private int x =3;  //外部成员
        class Inner
        {
                int x = 4;             //内部成员
                void function()
                {
                        int x = 6 ;        //内部局部成员
                        System.out.println(x);
                }
        }
}
回复 使用道具 举报
应该没啥区别,使用外部类,就必须加this
回复 使用道具 举报
其实是没区别,但如果是匿名内部了和静态内部类就不可以这样了
回复 使用道具 举报
本帖最后由 西门吹风 于 2014-6-5 00:15 编辑

如下代码,当有同名变量要用引用指定变量,才能访问到想要问的变量,如果只写x,个人理解会从内层向外找,内层有,就用内层的,没有就向外层找,没有再向外层找.........
  1. class  ThisTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Outer a=new Outer();
  6.                 a.method();
  7.                 Outer2 b=new Outer2();
  8.                 b.method();
  9.         }
  10. }
  11. class Outer
  12. {
  13.         int x =3;
  14.         void method()
  15.         {
  16.                 class Inner
  17.                 {
  18.                         int x=2;
  19.                         void function()
  20.                         {
  21.                                 int x=1;
  22.                                 System.out.println(Outer.this.x);  //打印3,外部类的成员变量
  23.                                 System.out.println(this.x);            //打印2,内部类的成员变量
  24.                                 System.out.println(x);                   //打印1,方法内的变量
  25.                         }               
  26.                 }
  27.                 new Inner().function();
  28.         }
  29. }
  30. class Outer2
  31. {
  32.         int x =3;
  33.         void method()
  34.         {
  35.                 class Inner
  36.                 {
  37.                         int x=2;
  38.                         void function()
  39.                         {
  40.                                 System.out.println(x);     //打印2,方法中没有,向外层找到成内部类的成员变量
  41.                         }               
  42.                 }
  43.                 new Inner().function();
  44.         }
  45. }
复制代码




点评

谢谢,我终于明白了  发表于 2014-6-5 22:52

评分

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

查看全部评分

回复 使用道具 举报 1 0
这里确实没有任何的区别,在this之前有一句隐含的代码!!,但是,再有内部成员变量和内部局部变量的时候,这两种写法就不一样了哦,具体 访问哪个变量,请看我的楼上!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马