局部匿名类

Q:

interface Inter
{
    void show();
}

class Outer
{
    //补齐代码,完成主方法中定义的功能
    
}

class Test
{
    public static void main(String[] args)
    {
        Outer.method().show(); //自己名字
    }
}

A:

interface Inter{
    void show();
}

class Outer{
    public static Inter method() {
        return new Inter(){
            @Override
            public  void  show() {
                System.out.println("sflik");     
            }    
        };       
    }  
}

public class Test{
    public static void main(String[] args) {
        Outer.method().show();
    }
}
原文地址:https://www.cnblogs.com/sflik/p/4753117.html