gpt4 book ai didi

java - 从接口(interface)实现通用方法?

转载 作者:行者123 更新时间:2023-12-02 15:58:10 25 4
gpt4 key购买 nike

我正在尝试实现一个自行创建的通用接口(interface)“BoundedQueue”,以数组作为底层结构。当我编译部分完成的类“BoundedQueueArray”时,出现错误:

3 errors found:
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java [line: 11]
Error: csc143.data_structures.BoundedQueueArray is not abstract and does not override abstract method insert(java.lang.Object) in csc143.data_structures.BoundedQueue
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java [line: 20]
Error: generic array creation
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java [line: 32]
Error: name clash: insert(T) in csc143.data_structures.BoundedQueueArray and insert(T) in csc143.data_structures.BoundedQueue have the same erasure, yet neither overrides the other

这是类(class):

package csc143.data_structures;

public class BoundedQueueArray<T> implements BoundedQueue {

// elements stored in array
private T[] elements;
// the number of elements currently in the queue
private int numElems;

public BoundedQueueArray(int capacity) {
// instantiate and bind to reference
elements = new T[capacity];

numElems = 0;
}

/**
* This method inserts the specified element, unless the
* queue is full.
*
* @param o The element to be inserted.
* @throws FullQueueException If the queue is full.
*/
public void insert(T o) throws FullQueueException {
if(numElems < elements.length) {
elements[numElems] = o;
numElems++;
} else { // queue is full, cannot add element
throw new FullQueueException("Queue is full.");
}

}

/**
* This method returns the element at the front of the
* queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T front() throws EmptyQueueException {

}

/**
* This method retrieves and removes the element at the front
* of the queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T remove() throws EmptyQueueException {
if(length() == 0) {
throw new EmptyQueueException("Queue is empty.");
}

}

/**
* This method reports whether or not the queue contains
* element(s).
*
* @return If one or more element exists or not.
*/
public boolean hasMember() {
return length() > 0;
}

/**
* This method reports whether the queue has space to add
* element(s).
*
* @return If space exists or not.
*/
public boolean hasSpace() {
return elements.length - length() > 0;
}

/**
* This method returns the capacity of the queue.
*
* @return The capacity of the queue.
*/
public int capacity() {
return elements.length;
}

/**
* This method returns the current length of the queue.
*
* @return The length of the queue.
*/
public int length() {
return numElems;
}

/**
* This method provides a string representation of the queue.
*
* @return The String representation of the queue.
*/
public String toString() {

}

}

这是它实现的接口(interface):

package csc143.data_structures;

public interface BoundedQueue<T> {

/**
* This method inserts the specified element, unless the
* queue is full.
*
* @param o The element to be inserted.
* @throws FullQueueException If the queue is full.
*/
public void insert(T o) throws FullQueueException;

/**
* This method returns the element at the front of the
* queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T front() throws EmptyQueueException;

/**
* This method retrieves and removes the element at the front
* of the queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T remove() throws EmptyQueueException;

/**
* This method reports whether or not the queue contains
* element(s).
*
* @return If one or more element exists or not.
*/
public boolean hasMember();

/**
* This method reports whether the queue has space to add
* element(s).
*
* @return If space exists or not.
*/
public boolean hasSpace();

/**
* This method returns the capacity of the queue.
*
* @return The capacity of the queue.
*/
public int capacity();

/**
* This method returns the current length of the queue.
*
* @return The length of the queue.
*/
public int length();

/**
* This method provides a string representation of the queue.
*
* @return The String representation of the queue.
*/
public String toString();

}

最佳答案

  1. 您无法创建泛型类型参数的数组。
  2. 您不能声明一个以类型参数作为参数的方法,该方法在删除后将与现有方法的签名相匹配。发生这种情况是因为您正在实现原始类型。

所以,你应该像这样声明你的类 -

public class BoundedQueueArray<T> implements BoundedQueue<T>

并从构造函数中删除以下数组创建代码 -

elements = new T[capacity];

如果您可以使用 List ( interfaceimplementation )代替数组(请参阅 Effective Java 、 Item 25),并远离原始类型,那就更好了尽可能多(请参阅 Effective Java,第 23 项)。

示例 -

private List<T> elements;    // convert array to list

和 -

elements = new ArrayList<T>();    // create instance like this

关于java - 从接口(interface)实现通用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988360/

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