SCJP 1.6版考題 016
出自 陳富國維基館
5. class Atom{ 6. Atom(){System.out.print("atom ");} 7. } 8. class Rock extends Atom{ 9. Rock(String type){System.out.print(type);} 10. } 11. public class Mountain extends Rock{ 12. Mountain(){ 13. super("granite "); 14. new Rock("granite "); 15. } 16. public static void main(String[] a){new Mountain();} 17. } What is the result? A. Compilation fails. B. atom granite C. granite granite D. atom granite granite E. An exception is thrown at runtime. F. atom granite atom granite
解答
Ans: F
解說:
記住, 若建構子中未寫this或super的呼叫敘述,編譯器會在建構子的首行加上super(), 以此例來說,建立Mountain物件時,先叫用super(“granite”),呼叫其父類別Rock的建構子, 此建構子會再呼叫其父類別Atom的建構子,因此,會先印出atom…
|