这是一道拓展题,我在练的时候写的时候写不出来,不知道为啥中间是这么写的:
这是答案:
public class TestAnonymous2 {
public static void main(String[] args) {
Test.function().method();
}
}
interface Inter{
void method();
}
class Test{
public static Inter function(){
return new Inter(){
@Override
public void method() {
System.out.println("测试");
}};
}
}
以下是我的理解:我看main方法里面用类名调用function()方法,说明这个方法是static修饰的,然后后面有接着个method(),那方法是在function()方法的里面,里面的new Inter是为了不让原来Test不用写Implements Inter 而这么写来实现method方法的么?