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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© vaqh 中级黑马   /  2014-7-27 21:38  /  1635 人查看  /  17 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class A
{
   int x = 1;
   class B {
  int x = 2;
  void func() {
                    int x = 3;
                  System.out.println( ? );
                }
          }
  }
在打印语句中如何打印这3个x变量?为什么呢???

17 个回复

倒序浏览
class A{
        int x = 3;
        class B{
                int x = 4 ;
                void function(){
                        int x = 5;
                        System.out.println("x=" + x);
                        System.out.println("x=" + this.x);
                        System.out.println("x=" + new A().x);

                }
        }
        void show(){
                B b = new B();
                b.function();
        }
}






class  Demo_Test{
        public static void main(String[] args) {
                A a = new A();
                a.show();
        }
}
我也很菜,自己打的。。
回复 使用道具 举报
闪电博尔特 来自手机 中级黑马 2014-7-27 22:00:10
藤椅
基础测试还有重复的啊
回复 使用道具 举报
DarkSky 发表于 2014-7-27 21:55
class A{
        int x = 3;
        class B{

谢谢,但是为什么不在Demo_Test类中直接调用function方法呢
回复 使用道具 举报
闪电博尔特 发表于 2014-7-27 22:00
基础测试还有重复的啊

???你也是这个啊
回复 使用道具 举报
vaqh 发表于 2014-7-27 22:00
谢谢,但是为什么不在Demo_Test类中直接调用function方法呢

外部类要想访问内部类,必须要建立内部类的对象。不能直接访问。
回复 使用道具 举报
vaqh 中级黑马 2014-7-27 22:53:35
7#
DarkSky 发表于 2014-7-27 22:47
外部类要想访问内部类,必须要建立内部类的对象。不能直接访问。

额,,谢谢了
回复 使用道具 举报
你好吗 来自手机 中级黑马 2014-7-27 23:39:48
8#
vaqh 发表于 2014-7-27 22:00
谢谢,但是为什么不在Demo_Test类中直接调用function方法呢

这样也可以的
回复 使用道具 举报
围观学知识~~~~~
回复 使用道具 举报
vaqh 中级黑马 2014-7-27 23:45:03
10#

那么如何调用呢
回复 使用道具 举报
DarkSky 发表于 2014-7-27 22:47
外部类要想访问内部类,必须要建立内部类的对象。不能直接访问。

应该要加上非静态吧?
回复 使用道具 举报
David.L 发表于 2014-7-27 23:56
应该要加上非静态吧?

对,静态的可以调用。。呵呵,我说的有漏洞。
回复 使用道具 举报
本帖最后由 David.L 于 2014-7-28 01:02 编辑
DarkSky 发表于 2014-7-27 21:55
class A{
        int x = 3;
        class B{

发现你的代码有问题哦,打印里面的你使用了new A(),这时就新建了一个新对象了,不是主函数里的对象a了
也就是这个new A().x已经不是你的代码里原来对象里的=3的那个x了

附上我写的代码,仅供参考
  1. class A
  2. {
  3.         int x = 1;
  4.         class B
  5.         {
  6.                 int x = 2;
  7.                 void func()
  8.                 {
  9.                         int x = 3;
  10.                         System.out.println("____"+getA()+"____"+this.x+"____"+x);
  11.         
  12.                         //或者将x=1用static修饰,这样直接可以用A.x引用,就不需要getA方法了
  13.                 }
  14.         }
  15.         int getA()
  16.         {
  17.                 return x;
  18.         }
  19. }
  20.   

  21. public class Test
  22. {
  23.         public static void main(String[] arg)
  24.         {
  25.                 A.B b=new A().new B();
  26.                 b.func();
  27.         }
  28. }


  29. //输出结果:____1____2____3
复制代码
回复 使用道具 举报 1 0
System.out.println(x);
System.out.println(this.x );
System.out.println(A.this.x);
回复 使用道具 举报
vaqh 中级黑马 2014-7-28 08:49:28
15#
hjfeng1987 发表于 2014-7-28 08:47
System.out.println(x);
System.out.println(this.x );
System.out.println(A.this.x);

哦,正解
回复 使用道具 举报
vaqh 发表于 2014-7-27 22:25
???你也是这个啊

是的啊啊
回复 使用道具 举报
一看我也不懂,立马补知识
回复 使用道具 举报
vaqh 中级黑马 2014-7-28 19:16:53
18#
tiandzwx 发表于 2014-7-28 14:05
一看我也不懂,立马补知识

加    油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马