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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© opo110 中级黑马   /  2016-2-24 11:51  /  466 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


        匿名内部类面试题:
                按照要求,补齐代码
                        interface Inter { void show(); }
                        class Outer { //补齐代码 }
                        class OuterDemo {
                                public static void main(String[] args) {
                                          Outer.method().show();
                                  }
                        }
                        要求在控制台输出”HelloWorld”

1 个回复

倒序浏览

回帖奖励 +1

interface Inter {
    void show();
    // public abstract
}

class Outer {
    // 补齐代码
    public static Inter method() {
        // 子类对象 -- 子类匿名对象
        return new Inter() {
            public void show() {
                System.out.println("HelloWorld");
            }
        };
    }
}

class OuterDemo {
    public static void main(String[] args) {
        Outer.method().show();
    /*
        1:Outer.method()可以看出method()应该是Outer中的一个静态方法。
        2:Outer.method().show()可以看出method()方法的返回值是一个对象。
            又由于接口Inter中有一个show()方法,所以我认为method()方法的返回值类型是一个接口。
    */
    }
}

评分

参与人数 1黑马币 +2 收起 理由
opo110 + 2 很给力!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马