Given:
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);
答案:AE
參考:6-5 繼承
------------------------------------------------------
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); //父類別沒有三個參數的建構式
沒有留言:
張貼留言