SCJP題庫第089題
1. public class Car{
2. private int wheelCount;
3. private String vin;
4. public Car(String vin){
5. this.vin = vin;
6. this.wheelCount = 4;
7. }
8. public String drive(){
9. return "zoom-zoom";
10. }
11. public String getInfo(){
12. return "VIN: " + vin + "wheels: " + wheelCount;
13. }
14.}
And:
1. public class MeGo extends Car{
2. public MeGo(String vin){
3. this.wheelCount = 3;
4. }
5. }
What two must the programmer do to correct the compilation errors? (Choose two.)
A. insert a call to this() in the Car constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to super() in the MeGo constructor
D. insert a call to super(vin) in the MeGo constructor
E. change the wheelCount variable in Car to protected
F. change line 3 in the MeGo class to super.wheelCount = 3;
Ans: DE
解說:
要可以編譯必須插入super(vin)在MeGo建構子裏
因為若沒有這個呼叫,編譯器會插入一個super()在MeGo建構子裏,而Car又沒有一個有空參數的建構子,這樣會錯!
但插入super(vin)後,這個vin不是自己宣告的變數,是Car的變數,因此要把private的存取屬性換掉(public/default也可以)
Comments