2013-05-29 38 views
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试用它,失败了。 它是一种新的语法或简单的错字?

+3

*“我尝试它的Java6,失败” * - 你能更具体说明究竟是什么失败? –

+4

合法的语法,而不是全新的。它指定了“任一”方法的类型参数。 –

回答

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

+0

对不起,我没有仔细阅读通用文档,错误也是。 错误说:该方法要么(Matcher )未定义的类型CombinableMatcher。 CombinableMatcher确实没有那种方法。也许是关于版本的问题。 – user2431811