Given:
10. interface Foo{int bar();}
11. public class Sprite{
12. public int fubar(Foo foo){return foo.bar();}
13. public void testFoo(){
14. fubar(
15. //insert code here
16. );
17. }
18. }
Which code, inserted at line 15, allows the class Sprite to compile?
A. Foo{public int bar(){return 1;}}
B. new Foo{public int bar(){return 1;}}
C. new Foo(){public int bar(){return 1;}}
D. new class Foo{public int bar(){return 1;}}
答案:C
參考:8-5 內部類別
---------------------------------------
14 行要 呼叫 12 行public int fubar(Foo foo) , 此方法的參數是 Foo類型.
C是用暱名類別實做10. interface Foo{int bar();} 就可當作參數來傳遞.
沒有留言:
張貼留言