2015-10-15 93 views
0

下面是用於CleverSearchTimer與接口CleverSearchTimer無法實例化CleverSearchTimer類型?

public class CleverSearchTimerTest { 

private static SimpleRandomCount srctest; 
private static CleverSearchTimer ssttest; 

@BeforeClass 
public static void setUpBeforeClass() { 
    srctest = new SimpleRandomCount(25); 
    ssttest = new CleverSearchTimer(); 
} 

@AfterClass 
public static void tearDownAfterClass() throws Exception { 
} 

@Before 
public void setUp() throws Exception { 
} 

@After 
public void tearDown() throws Exception { 
} 

public void findKthElementTestRegular() throws IndexingError{ 
    assertEquals(ssttest.findKthElement(srctest.array(),4),21); 
} 

public void FindKthElementTestBoundaryLow() throws IndexingError{ 
    assertEquals(ssttest.findKthElement(srctest.array(),25), 0); 
} 

public void FindKthElementTestBoundaryHigh() throws IndexingError{ 
    assertEquals(ssttest.findKthElement(srctest.array(),1), 24); 
} 

public void FindKthElementTestErroneous() throws IndexingError{ 
    equals(ssttest.findKthElement(srctest.array(), 30)); 
} 

@Test 
public void test() { 
    fail("Not yet implemented"); 
} 

} 

CleverSearchTimer測試類:

public interface CleverSearchTimer { 
/** 
* Find the kth largest element in an array of ints 
* @param array — array of ints 
* @param k — index of the element to be found 
* @return — kth largest int in array 
* @throws IndexingError — if k is not a valid index 
*/ 
    public int findKthElement(int[] array, int k) 
    throws IndexingError; 



/** Find the time it takes to find the kth element 
* @param array — array of ints 
* @param k — the index of the element to be found 
* @return — the time taken to find the kth largest element 
* @throws IndexingError — if k is not a valid index 
*/ 

    public double time(int[] array,int k) 
    throws IndexingError; 
} // end of interface TimedSearch 

就行出現錯誤 「ssttest =新CleverSearchTimer();」 「無法實例化CleverSearchTimer類型」。

我想這是因爲你不能在一個測試類中實例化一個接口。還有另一種方法可以做到嗎?

感謝您的任何幫助。

回答

1

接口無法實例化。您必須創建一個實現該接口的類,然後實例化該類。匿名類是一個例外,但在這種情況下不適用。

0

是,創建一個實現接口

public MyClass implements MyInterface {...} 

要知道你將需要實現所有您在界面中指定的方法的類

相關問題