0

在这里,https ://github.com/junit-team/junit/wiki/Assertions ,看看:

public void testAssertThatHamcrestCoreMatchers() {
    assertThat("good", allOf(equalTo("good"), startsWith("good")));
    assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
    assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
    assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
    assertThat(new Object(), not(sameInstance(new Object())));
}

在第 5 行,CombinableMatcher.<Integer>有效吗?我用java6试了一下,失败了。是新语法还是简单的错字?

4

1 回答 1

0
public static void main(String[] args) {
    java.util.ArrayList<Box<Integer>> listOfIntegerBoxes =
      new java.util.ArrayList<>();
    BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
    BoxDemo.addBox(Integer.valueOf(20), listOfIntegerBoxes);
    BoxDemo.addBox(Integer.valueOf(30), listOfIntegerBoxes);
    BoxDemo.outputBoxes(listOfIntegerBoxes);
  }

泛型方法 addBox 定义了一个名为 U 的类型参数。通常,Java 编译器可以推断出泛型方法调用的类型参数。因此,在大多数情况下,您不必指定它们。例如,要调用泛型方法 addBox,您可以指定类型参数,如下所示:

BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);

来源:http ://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html

于 2013-05-29T09:40:18.300 回答