【发布时间】:2013-11-19 07:26:29
【问题描述】:
我正在准备考试,有以下问题:
考虑以下代码和继承层次结构:
public Deque<E> theMethod(E arg1, ArrayDeque<E> arg2)
{
return theOtherMethod(arg1, arg2);
}
D<|----E<|----F
Collection<|----------------------Deque<|----------------
^ ^ |
| | |
| |------ArrayDeque |
| | |
| | |
| | |
AbstractCollection<|------| |
| |
----AbstractList<|----------LinkedList
Considering only the classes shown above:
(a) What types of objects can you supply to theMethod as arguments?
(b) What could the declared parameter types of theOtherMethod be?
(c) What could the declared return type of theOtherMethod be?
希望 UML 是可读的,
我的答案如下,但我不确定它们是否正确,因为我不了解泛型如何影响可能的参数和返回类型。
(a) First param: E and F, second param only ArrayDeque
(b) First param: could be D or E, second param: could be Collection, Deque, ArrayDeque or AbstractCollection
(c) Collection or Deque
【问题讨论】:
-
请为
D、E和F使用不同的名称。目前我把它与类型参数混淆了。 -
Deque<E>和ArrayDeque<E>怎么样? -
我认为这意味着它们是泛型的?
-
你从哪里得到这个问题的?
Deque<E>不应该是Deque<Echo>,否则应该为方法声明类型参数E。 -
我理解
代表通用,这可能与 Echo 类无关?
标签: java oop generics inheritance