黑马程序员技术交流社区

标题: java基础(深入浅出版) 面试题总结之day10:面向对象--匿名内... [打印本页]

作者: huang_bing_hua    时间: 2016-2-26 20:00
标题: java基础(深入浅出版) 面试题总结之day10:面向对象--匿名内...
  1. <div class="blockcode"><blockquote>/*
  2.         匿名内部类面试题:
  3.                 按照要求,补齐代码
  4.                         interface Inter { void show(); }
  5.                         class Outer { //补齐代码 }
  6.                         class OuterDemo {
  7.                                 public static void main(String[] args) {
  8.                                           Outer.method().show();
  9.                                   }
  10.                         }
  11.                         要求在控制台输出”HelloWorld”
  12. */
  13. interface Inter {
  14.         void show();
  15.         //public abstract
  16. }

  17. class Outer {
  18.         //补齐代码
  19.         public static Inter method() {
  20.                 //子类对象 -- 子类匿名对象
  21.                 return new Inter() {
  22.                         public void show() {
  23.                                 System.out.println("HelloWorld");
  24.                         }
  25.                 };
  26.         }
  27. }

  28. class OuterDemo {
  29.         public static void main(String[] args) {
  30.                 Outer.method().show();
  31.                 /*
  32.                         1:Outer.method()可以看出method()应该是Outer中的一个静态方法。
  33.                         2:Outer.method().show()可以看出method()方法的返回值是一个对象。
复制代码








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