我學習的SCJP,並在學習期間我發現,起初看起來很簡單的運動後,但我失敗瞭解決它,我不明白的答案。演習(從OCP Java SE 6的程序員考試題庫,伯特·貝茨和凱西塞拉利昂採取的),說以下內容:調用從主的私人包方法調用構造函數
考慮:
import java.util.*;
public class MyPancake implements Pancake {
public static void main(String[] args) {
List<String> x = new ArrayList<String>();
x.add("3");x.add("7");x.add("5");
List<String> y = new MyPancake().doStuff(x);
y.add("1");
System.out.println(x);
}
List<String> doStuff(List<String> z) {
z.add("9");
return z;
}
}
interface Pancake {
List<String> doStuff(List<String> s);
}
What is the most likely result?
A. [3, 7, 5]
B. [3, 7, 5, 9]
C. [3, 7, 5, 9, 1]
D. Compilation fails.
E. An exception is thrown at runtime
答案是:
D is correct. MyPancake.doStuff() must be marked public. If it is, then C would be
correct.
A, B, C, and E are incorrect based on the above.
我的猜測是C,因爲doStuff方法在MyPancake類中,所以主方法應該可以訪問它。
反思的問題,呼籲從靜態上下文新時,它可能沒有訪問私有方法,如果doStuff是私人。這是真的?我不確定這一點。
但無論如何,我仍然認爲這將有機會獲得包私人doStuff方法。 我想我錯了,但我不知道爲什麼。
你能幫我嗎?
謝謝!
非常感謝!我專注於回顧我的筆記,這本書,但我沒有嘗試編譯它。我應該記住,界面成員總是公開的。 – pablof