3、关于匿名内部类一个小题目(补全代码)

来源:互联网 发布:linux string 编辑:程序博客网 时间:2024/06/07 01:35
/*匿名内部类面试题:按照要求,补齐代码interface Inter { void show(); }class Outer { //补齐代码 }class OuterDemo {public static void main(String[] args) {  Outer.method().show();  }}要求在控制台输出”HelloWorld”*/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()方法的返回值类型是一个接口。*/}}


原创粉丝点击