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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不存在的奇迹 中级黑马   /  2015-4-20 18:19  /  751 人查看  /  15 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

成员内部类的面试题
                要求输出30,20,10
                class Outer {
                        public int num = 10;
                        class Inner {
                                public int num = 20;
                                public viod show() {
                                        int num  = 30;
                                        System.out.println(???);
                                        System.out.println(???);
                                        System.out.println(???);
                                                                }
                                        }
                                }

15 个回复

倒序浏览
num,this.num,Outer.this.num。
回复 使用道具 举报 1 0
  1. class Outer
  2. {
  3.     public int num = 10;
  4.     class Inner
  5.         {
  6.         public int num = 20;
  7.         public void show()
  8.                 {
  9.             int num  = 30;
  10.             System.out.println(num);
  11.             System.out.println(this.num);
  12.             System.out.println(Outer.this.num);
  13.         }
  14.         }
  15. }
  16. class InnerTest
  17. {
  18.          public static void main(String[] args)
  19.          {
  20.                  /*Outer.Inner oi=new Outer().new Inner();
  21.                  oi.show();*/
  22.                  new Outer().new Inner().show();
  23.          }
  24. }
复制代码
回复 使用道具 举报
刚刚看了下  成员内部类 和 静态内部类 总结如下

外部类中的静态方法           只能直接调用静态方法      内部类必须是静态的才能直接new (要不只能通过外部类对象创建内部类对象)

外部类的非静态方法           都能调用                        内部类都能直接new (也可以通过外部类对象间接创建内部类对象)
回复 使用道具 举报
谢谢分享哈
回复 使用道具 举报
谢谢楼主,好久没看到这么好的贴了
回复 使用道具 举报
感谢楼主
回复 使用道具 举报
这个方面的话题的 都讨论一下
回复 使用道具 举报

大家一起学习 谢谢
回复 使用道具 举报
Outher.this     学习了
回复 使用道具 举报
呵呵,不错
回复 使用道具 举报
棒,将薄弱的薄弱的知识点加已了解巩固
回复 使用道具 举报
num  this.num  Outer.this.num
回复 使用道具 举报
很有代表性!
回复 使用道具 举报
经典!!!!
回复 使用道具 举报
非常经典,受教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马