2013-05-29 45 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