gpt4 book ai didi

java - 无法实例化 CleverSearchTimer 类型?

转载 作者:行者123 更新时间:2023-12-01 11:03:29 24 4
gpt4 key购买 nike

下面是 CleverSearchTimer 和 CleverSearchTimer 接口(interface)的测试类

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");
}

}

聪明搜索定时器:

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 = new CleverSearchTimer();”行出现错误“无法实例化类型 CleverSearchTimer”。

我认为这是因为你无法在测试类中实例化接口(interface)。还有其他方法吗?

感谢您的帮助。

最佳答案

接口(interface)无法实例化。您必须创建一个实现该接口(interface)的类,然后实例化该类。匿名类是一个异常(exception),但不适用于这种情况。

关于java - 无法实例化 CleverSearchTimer 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com