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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© runner 中级黑马   /  2016-5-11 22:25  /  411 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Inter { void show(); }
                class Outer { //补齐代码 }
                class OuterDemo {
                        public static void main(String[] args) {
                                  Outer.method().show();
                          }
                }
                要求在控制台输出”HelloWorld”

4 个回复

倒序浏览
        public static  Inter method() {
                class Inner implements Inter {
                        public void show () {
                                System.out.println("HelloWorld");
                        }
                }
                Inter inter = new Inner();
                return inter;
        }
}
回复 使用道具 举报
        public static void main(String[] args) {
                Outer.method().show();                                        //链式编程,每次调用方法后,还能在继续调用,证明返回的是对象               
        }
}

interface Inter {
        void show();
}

class Outer {
        public static Inter method() {                               
                return new Inter() {
                        public void show() {
                                System.out.println("helloworld");
                        }
                };
        }
}
回复 使用道具 举报
Outer.method().show();

能通过类名点调用的method()应该是个静态方法,

方法名method()能调用show()方法,说明他返回值是一个对象,匿名内部类就可以实现了
回复 使用道具 举报
看一看。。。。。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马