2017-02-12 120 views
1

我开始使用Java的泛型,似乎缺少一个关键组件。原始类型/ T无法解析

首先,我做了一些对原材料型阅读需要的参数,实现没有太多做的,因为它们是通用的,但我的问题是BagInterfaceLinkedBag之间的相互作用:

package Chapter3; 

public interface BagInterface<T> { 

/** Gets the current number of entries in the bag. 
* @return the integer number of entries in the bag. */ 
public int getCurrentSize(); 

/** Sees whether this bag is full. 
*@return true if the bag is full, or false if not. */ 
public boolean isFull(); 

/** Sees whether the bag is empty. 
*@return true if bag is empty, or false if not. */ 
public boolean isEmpty(); 

/** Adds new entry to this bag. 
*@param newEntry the object to be added as a new entry 
*@return if the addition was successful, or false if not. */ 
public boolean add(T newEntry); 

/** Removes one unspecified entry from this bag, if possible. 
*@return either the removed entry, if the removal was successful, or null. */ 
public T remove(); 

/** Removes one occurrence of a given entry from this bag. 
*@param anEntry the entry to be removed 
*@return true id the removal was successful, or false if not. */ 
public boolean removal(T anEntry); 

/** Removes all entries from this bag. */ 
public void clear(); 

/** Counts the number of times a given entry appears in this bag. 
*@param anEntry the entry to be counted 
*@return the number of times anEntry appears in the bag. */ 
public int getFrequencyOf(T anEntry); 

/** Tests whether this bag contains a given entry. 
*@param anEntry the entry to locate 
*@return true if this bag contains anEntry, or false if not. */ 
public boolean contains(T anEntry); 

/**Retrieves all entries that are in this bag. 
*@return a newly allocated array of all the entries in the bag */ 
public T[] toArray(); 
} 

的两个错误做有T没有得到解决

package Chapter3; 

public class LinkedBag implements BagInterface { 

// reference to first node 
private Node firstNode; 
private int numberOfEntries; 

// default constructor 
public LinkedBag() { 

firstNode = null; 
numberOfEntries = 0; 
} 

// second constructor 
(error occurs here) public LinkedBag(T[] item, int numberOfItems) { 
this(); 
for(int index = 0; index < numberOfItems; index++) 
add(item[index]); 
}` 

另一个是是与get.data但我相信,也有以T没有解决

(error occurs here) result[index] = currentNode.getData(); 
index++; 
currentNode = currentNode.getNextNode(); 
}// end while 
return result; 
}// end is full 

我已经转录完整的.java文件,注意是否需要更多信息,但我试图保持它的特定和简洁。

+0

你会得到什么错误?你能分享他们的确切文字吗?对T – Mureinik

+0

错误是 “T不能被解析为一个类型” 在错误的getData() “从类型LinkedBag.Node方法的getData()是指缺少类型T” – DR4QU3

+0

什么行会产生这个错误? – Mureinik

回答

2

LinkedBag在您分享的代码中实现原始BagInterface。如果你想参考它的类型说明,你也应该添加类型参数到LinkedBag,并让它以某种方式参考BagInterface类型。例如:

public class LinkedBag<T> implements BagInterface<T> { 
// Here --------------^---------------------------^ 
+0

谢谢我,昨晚正在这个工作,并打墙,我在工作,所以我无法测试它,但我会这样做,看看我的问题解决了..我虽然也许我只是没有将类连接在一起,但它们存在于同一个包中(再次我的编译器知识也是有限的) 只在本学期开始使用eclipse – DR4QU3