即使這個問題已經回答了,因爲主題行約(「類型參數T隱藏類型T「),只是想補充一點,這個警告並不總是意味着在類路徑(或內部類)中聲明瞭一個類或實際類型」T「,如所有答案/註釋所示。
考慮以下幾點:
public class Cache<K extends Comparable<K>,V> {
private final ConcurrentHashMap<K,V> _concurrentMap = new ConcurrentHashMap<K,V>();
public <K,V> void add(K key ,V value){ // Compiler warning - The type parameter V is hiding type V (as also for K)
_concurrentMap.put(key, value); //Compiler error - K and V passed to add are now being considered different
//from whats declared for _concurrentMap variable
}
public V get(K key){
return _concurrentMap.get(key);
}
public int size(){
return _concurrentMap.size();
}
}
由於K,V是在類級別聲明,該方法還添加 「繼承」 了。所以你最終會得到同樣的警告。
下面消除當然
public <E,F> void add(K key ,V value){
_concurrentMap.put(key, value);
}
的警告和編譯器錯誤而我原來的方法是這樣的。(不需要額外的泛型類型爲我的方法)
public void add(K key ,V value){
_concurrentMap.put(key, value);
}
在我的情況下,Eclipse已經自動導入了'org.apache.poi.ss.formula.functions.T',這導致了警告。 – 2014-07-07 11:57:46