2009年2月11日 星期三

第89題

Given:
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;
答案:DE
參考:6-5 繼承、7-3 存取修飾詞的使用

-----------------------------------------------------

D. 加個 super(vin) 在MeGo的建構式裡, 如果不加程式會自己加super(); 但是父類別裡沒有無參數的建構子.
E. MeGo 裡面是存取 父類別 Car的 WheelCount,但是 Car的 WheelCount 是 private,是無法存取.
所以改成protected.

沒有留言:

張貼留言