【面试重点】匿名内部类
在标识处编写程序输出 HelloWorld
1 public class test { 2 public static void main(String[] args) { 3 Outer.method().show(); 4 } 5 } 6 7 interface Inter { 8 void show(); 9 } 10 class Outer { 11 // 编写程序 12 }
答案
1 class Outer { 2 public static Inter method() { 3 return new Inter() { 4 @Override 5 public void show() { 6 System.out.println("HelloWorld"); 7 } 8 }; 9 } 10 }
重点: method方法的返回值必须是Inter,不能是void,否则会出现如下提示 (且注意line3的格式)

void是无法引用show()的,必须是一个对象

浙公网安备 33010602011771号