黑马程序员技术交流社区

标题: 补齐代码 内部类面试题! [打印本页]

作者: 繁华落幕    时间: 2015-6-15 22:11
标题: 补齐代码 内部类面试题!
本帖最后由 繁华落幕 于 2015-6-24 22:19 编辑

答案 和  讲解  最好有注释
interface Inter
                {
                        void show();
                }

                class Outer
                {
                        //补齐代码,完成主方法中定义的功能
                }

                class Test
                {
                        public static void main(String[] args)
                        {
                                Outer.method().show();
                        }
                }


作者: iFmmer    时间: 2015-6-15 22:21
  1.     interface Inter
  2.     {
  3.         void show();   
  4.     }

  5.     class Outer
  6.     {
  7.         public static Inter method() {
  8.             class method implements Inter{
  9.                 public void show() {
  10.                 }
  11.             }
  12.             return new method();
  13.         }
  14.     }

  15.     class Test
  16.     {
  17.         public static void main(String[] args)
  18.         {
  19.             Outer.method().show();
  20.         }
  21.     }


复制代码

我唯独内部类学的不好,应该是写错了,我弱到连注释都写不上,这里当练手了,也希望各位大神能给我只出错误!

作者: shuaiqi_P_D    时间: 2015-6-15 22:35
楼上的基本正确。 内部类的命名不规范
作者: liu100chao    时间: 2015-6-15 22:44
public static void method()
作者: l李伟    时间: 2015-6-15 22:54
我只是看看
作者: liu100chao    时间: 2015-6-15 23:00
首先从这个Outer.method()可以看出method()是类OUT中的静态方法 ,而Outer.method()可以直接调用show();方法,说明Outer.method()运算以后的结果是一个 Inter对象,应为只有Inter对象对象才能调用show()方法
作者: liu100chao    时间: 2015-6-15 23:07
此为匿名内部类的写法!!
class Outer
{
        static Inter method()
        {
                return new Inter()
                {
                        public void show()
                        {
                                System.out.println("method run");
                        }
                };
        }

}

作者: 安安安    时间: 2015-6-15 23:20
  1. public class Demo1 {
  2.         public static void main(String[] args)
  3.         {
  4.                 Outer.method().show();
  5.         }
  6. }
  7. interface Inter
  8. {
  9.         void show();
  10. }

  11. class Outer
  12. {
  13.     //补齐代码,完成主方法中定义的功能
  14.          static Inter method(){
  15.                  return new Inter(){
  16.                          public void show()
  17.                          {
  18.                                  System.out.println("show");
  19.                          }
  20.                  };
  21.          }
  22. }
复制代码

作者: micro_hx    时间: 2015-6-16 09:55
我只是看看。。。。谢谢楼主分享啊。。。。
作者: lucien_he    时间: 2015-6-16 10:16
学习一下
作者: 繁华落幕    时间: 2015-6-16 10:29
本帖最后由 繁华落幕 于 2015-6-16 10:41 编辑

昨天刚搞懂
作者: 繁华落幕    时间: 2015-6-16 10:32
本帖最后由 繁华落幕 于 2015-6-16 10:42 编辑
iFmmer 发表于 2015-6-15 22:21
我唯独内部类学的不好,应该是写错了,我弱到连注释都写不上,这里当练手了,也希望各位大神能给我只出错 ...

嗯嗯  我也是内部类学的不太好  一看到这玩意脑袋就发蒙 唉
作者: mishisanyi    时间: 2015-6-16 10:41
  1. package inerExam;


  2. interface Inter
  3. {
  4.         void show();
  5. }

  6. class Outer
  7. {
  8.         public static Inter method()
  9.         {
  10.                 return new Inter() {
  11.                                
  12.                                 @Override
  13.                                 public void show() {
  14.                                         // TODO 自动生成的方法存根
  15.                                         System.out.println("show");
  16.                                 }
  17.                         };
  18.         }
  19. }

  20. public class InnerDemo
  21. {
  22.         public static void main(String[] args)
  23.         {
  24.                Outer.method().show();
  25.         }
  26. }
复制代码

作者: 繁华落幕    时间: 2015-6-16 10:43
mishisanyi 发表于 2015-6-16 10:41

谢谢了   eclipse 嘿嘿




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2