黑马程序员技术交流社区

标题: 匿名内部类练习 [打印本页]

作者: keto    时间: 2015-6-26 19:46
标题: 匿名内部类练习
/*
        interface Inter {
                        void show();
        }
        class Outer {
                //补齐代码
        }
        class OuterDemo {
            public static void main(String[] args) {
                      Outer.method().show();
                  }
        }
        要求在控制台输出”HelloWorld”

*/
interface Inter {
                void show();
}
class Outer {
        //补齐代码
        public static Inter method(){
                //定义一个有名字的内部类;
        /*        class B implements Inter
                {
                        public void show(){
                                System.out.println("HelloWorld");
                        }
                }
                return new B();
        */
                return new Inter(){
                        public void show(){
                                System.out.println("HelloWorld");
                        }
                };
        }
}

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





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