歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux文化 >> 有道Scjp的模擬題搞不懂

有道Scjp的模擬題搞不懂

日期:2017/2/27 12:12:06   编辑:Linux文化

>>> 此貼的回復 >> 模擬器不是有給出答案的麼?

>>> 此貼的回復 >> 答案的解釋是這樣子的 D is correct. A subclass in another package can only access protected members in the superclass via references of its own type or a subtype. In the question above the class SubclassY defines two instance variables, one of its own type (objY) and another of its superclass's type (objX). Hence there is no problem in accessing the method superclassMethodX() at line 9 but there will be a compilation error at line 11 as objX is not allowed to access any protected member of the SuperclassX.

但我的答案和測試都是會在line 9出問題啊,而不是在line11

>>> 此貼的回復 >> protected只能讓同一個包或其它包的子類訪問.上面的代碼在編譯時編譯器就會檢查objX的聲明類型(這裡objX為SuperclassX類型).因為上面兩個類是在不同包的,但又不是其子類所以編譯器認為objX不能訪問protected int superclassVarX. 你把SuperclassX objX = new SubclassY()改成SubclassY objX = new SubclassY()後編譯就通過了 line9是沒問題的啊 (個人意見說的不對請原諒)

>>> 此貼的回復 >> superclassMethodX()是父類包內可見的方法,子類並不在同一個包內的類應該是不可以調用的 我同意答案是A (個人意見)

>>> 此貼的回復 >> 11.i = objX.superclassVarX 注意這行 不是在packageX包中,objX也不是子類對象,所以不能編譯


Copyright © Linux教程網 All Rights Reserved