我在這里收到警告,說 Map 是原始型別。
// Map is a raw type. References to generic type Map<K,V> should be parameterized
Class<Map> c = Map.class;
但是如果我引數化型別,那么我會得到一個不匹配的結果:
// Type mismatch: cannot convert from Class<Map> to Class<Map<Object,Object>>
Class<Map<Object, Object>> c = Map.class;
我嘗試了一些變體,但我找不到任何方法來引數化運算式 ( Map.class) 的右側以滿足引數化的左側。
在這種情況下如何引數化Map.class?
在這種情況下,我將它傳遞給帶有簽名的方法:
public <T> T method(Class<T> type) {
return (T) otherMethod(type);
}
我只想提供Map型別,因為我不知道內部使用什么樣的地圖,這個細節并不重要。我正在嘗試解決呼叫該方法時收到的警告:
// Type safety: The expression of type Map needs unchecked conversion to conform to Map<String,Object>
Map<String, Object> a = method(Map.class);
uj5u.com熱心網友回復:
這會抑制呼叫站點的警告:
public static <T> T method(Class<? super T> type) {
return (T) otherMethod(type);
}
運算式上仍然存在未經檢查的強制轉換警告(T) otherMethod(type),但我相信這是不可避免的。
uj5u.com熱心網友回復:
Map.class是的Class<Map>,不是的Class<Map<Object, Object>。您可以使用Class<? super Map<Object, Object>> c = Map.class;,因為 everyMap<Object, Object>也是一個Map(但不是相反)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510456.html
標籤:爪哇仿制药原始类型
上一篇:具有單維和多維陣列的可變引數
