SCJP 1.6版考題 044

出自 陳富國維基館
於 2013年3月30日 (六) 07:42 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: 1. class Employee{ 2. String name; double baseSalary; 3. public Employee(String name, double baseSalary){ 4. this.name = name; 5. this.baseSalary = baseSalary; 6. ...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
1. class Employee{
2.   String name; double baseSalary;
3.     public Employee(String name, double baseSalary){
4.     this.name = name;
5.     this.baseSalary = baseSalary;
6.   }
7. }
8. public class SalesPerson extends Employee{
9.   double commission;
10.  public SalesPerson(String name,
11.      double baseSalary, double commission){
12.    //insert code here
13.  }
14.}


Which two code fragments, inserted independently at line 12, will compile? (Choose two.)

  A. super(name, baseSalary);
  B. this.commission = commission;
  C. super();
     this.commission = commission;
  D. this.commission = commission;
     super();
  E. super(name, baseSalary);
     this.commission = commission;
  F. this.commission = commission;
     super(name, baseSalary);
  G. super(name, baseSalary, commission);


解答


Ans:A E

解說:

由於父類別Employee沒有“無參數”的建構子,所以本程式要能編譯,必須要避免編譯器為SalesPerson的建構子加上super(),
於是就加A/E,加上明確的呼叫父類別的建構子Employee(String name, double baseSalary)