下面的问题来自 Kathy Sierra 和 Bert Bates 所著的 Java SCJP5 书籍。
给定一个声明为的方法:
public static <E extends Number> List<E> process(List<E> nums)
程序员想要使用这样的方法:
// INSERT DECLARATIONS HERE
output = process(input);
哪对声明可以放在 // INSERT DECLARATIONS HERE 处以允许代码编译? (选择所有适用的选项。)
A.
ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
B.
ArrayList<Integer> input = null;
List<Integer> output = null;
C.
ArrayList<Integer> input = null;
List<Number> output = null;
D.
List<Number> input = null;
ArrayList<Integer> output = null;
E.
List<Number> input = null;
List<Number> output = null;
F.
List<Integer> input = null;
List<Integer> output = null;
G. 以上都不是。
给出的正确答案是:B、E、F,书中的解释是:
“返回类型肯定声明为List,而不是ArrayList,所以A、D都是错误的......”
这是我不明白的...为什么返回类型必须仅为 List 而不是 ArrayList?就像参数可以是 ArrayList 那么为什么返回类型不能也是 arrayList 呢?
Thanks