面向物件_匿名內部類的面試題
阿新 • • 發佈:2018-12-26
/* 按照要求,補齊程式碼 interface Inter{ void sho(); } class Outer{ //補齊程式碼 } class OuterDemo{ public static void main(String[] args){ Outer.method().show(); } } 要求在控制檯輸出"HelloWorld" */ package cn.test; interface Inter{ void show(); //public abstract void show(); } 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(); /* a:Outer.method()可以看method()應該是Outer()中的一個靜態方法? b:Outer.method().show()可以看method()方法的返回值是一個物件。 又由於介面Inter中有一個show()方法,所以我認為method()方法的返回值型別是一個介面。 */ } }