我有3种方法:
//1 -- check one item
public static <T> void containsAtLeast(String message,
T expectedItem,
Collection<? extends T> found) {
if (!found.contains(expectedItem))
Assert.fail("...");
}
//2 -- check several items
public static <T> void containsAtLeast(String message,
Collection<? extends T> expectedItems,
Collection<T> found) {
for (T exptetedItem : expectedItems)
containsAtLeast(message, exptetedItem, found);
}
//3 -- check several items, without message parameter
public static <T> void containsAtLeast(Collection<? extends T> expectedItems,
Collection<? extends T> found) {
containsAtLeast(null, expectedItems, found);
}
Run Code Online (Sandbox Code Playgroud)
我希望该方法可以//3调用//2 但不会调用方法//1.我的期望是否有错误?
*我使用sdk 1.7.0_25和Eclipse 4.3*
ass*_*ias 12
第二个方法需要这种类型的expectedItems(? extends T)是一般类型的一个子类型found(T).
在第三种方法中,两种泛型类型之间没有子类型关系.他们都延伸T但可能是兄弟姐妹.
所以第二种方法无法调用.
示例:假设您使用这些类型调用第三个方法:
containsAtLeast(Collection<Integer> e, Collection<String> f)
Run Code Online (Sandbox Code Playgroud)
所以T你的第三种方法是Object.你的第一个方法也被调用T = Object了.