SCJP 1.6版考題 043

出自 陳富國維基館
於 2013年3月30日 (六) 07:43 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: 1. class Plant{ 2. private String name; 3. public Plant(String name){this.name = name;} 4. public String getName(){return name;} 5. } 6. public class Tree extends Plant{ 7...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
1. class Plant{
2.   private String name;
3.   public Plant(String name){this.name = name;}
4.   public String getName(){return name;}
5. }
6. public class Tree extends Plant{
7.   public void growFruit(){}
8.   public void dropLeaves(){}
9. }


What statement is true?
  A. The code will compile without changes.
  B. The code will compile if public Tree(){Plant();} is added to the Tree class.
  C. The code will compile if public Plant(){Tree();} is added to the Plant class.
  D. The code will compile if public Plant(){this("fern");} is added to the Plant class.
  E. The code will compile if public Plant(){Plant("fern");} is added to the Plant class.

解答


Ans: D

解說: Tree的預設建構子有super(),父類別必須有無參數的建構子