java基础面试题4--匿名内部类面试题


按照要求,补齐代码

要求:在控制台输出“Hello World”

interface Inter { 
    void show();
}

class Outer {
    //补齐代码:::::::::::::::
    public static Inter method(){
        //子类对象 -- 子类匿名对象
        return new Inter(){
            public void show(){
                syso("Hello World");
            }
        }
    }
}

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

    }
}


  1.由Outer.method()可以看出method()应该是Outer中的一个静态方法

  2.Outer.method.show()可以看出method()方法的返回值是一个对象;又由于Inter中有一个show()方法,所以method()方法的返回值类型是一个接口

原文地址:https://www.cnblogs.com/shiguangmanbu2016/p/5932832.html