从接口实现泛型方法?

时间:2013-08-01 08:04:37

标签: java

我正在尝试使用数组作为底层结构来实现自创建的通用接口“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

这是班级:

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() {

  }

}

这是它实现的界面:

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();

}

3 个答案:

答案 0 :(得分:7)

  1. 您无法创建泛型类型参数的数组。
  2. 您不能声明一个带有类型参数的方法作为其参数,该方法将在擦除后匹配现有方法的签名。这是因为您正在实现原始类型。
  3. 所以,你应该像这样声明你的课 -

    public class BoundedQueueArray<T> implements BoundedQueue<T>
    

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

    elements = new T[capacity];
    

    最好是使用Listinterfaceimplementation)代替数组(参见Effective Java,第25项),然后远离尽可能从原始类型(见Effective Java,第23项)。

    示例 -

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

    和 -

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

答案 1 :(得分:0)

private T[] elements;

这一行是问题所在。据我所知,不允许通用数组。

使用List代替

答案 2 :(得分:0)

正如我在评论中所说,去看看ArrayList的实现是否有一些想法

当java中的每个对象都实现Object时,在ArrayList中你可以看到元素存储在Object[]

private transient Object[] elementData;

如果你知道数组的类型,你可以毫无问题地初始化它

你的插入将完全相同,无论T是什么,它总是会继承Object

从ArrayList获取对象也很简单,你需要做的就是将它转换为你的泛型类型

     public E get(int index) {  
       RangeCheck(index); //irrevelant for your example
       return (E) elementData[index];
     }

如果我错过任何内容,请去查看here