我学习的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