我有一個關於Java中的genrics(可能很簡單)的問題。我有以下類:構造函數中的泛型Java類型錯誤
public class ValueCollection<Y> implements Collection<Y>
{
private Set<Entry<?, Y>> entries;
public ValueCollection(Set<Entry<?, Y>> entries)
{
this.entries = entries;
}
...
}
當我這樣調用構造函數:
return new ValueCollection<V>(entries);
我得到以下編譯器錯誤:
The constructor ValueCollection<V>(Set<Map.Entry<K,V>>) is undefined
如果我改變我的同班同學這樣的:
public class ValueCollection<X, Y> implements Collection<Y>
{
private Set<Entry<X, Y>> entries;
public ValueCollection(Set<Entry<X, Y>> entries)
{
this.entries = entries;
}
...
}
and my cons這個工程師打電話給:
return new ValueCollection<K, V>(this.entries());
編譯錯誤消失。我只是想知道爲什麼會這樣。謝謝您的幫助!
如果你在第一種情況下用'new ValueCollection(entries)'調用構造函數怎麼辦?你的第一堂課只有一個類型參數。 –
millimoose
2012-07-26 18:54:08