【发布时间】:2022-01-09 13:59:33
【问题描述】:
我有一个 HashMap
示例如下:
class Main extends HashMap<String, String>
{
public static void main(String[] args)
{
Main m = new Main();
m.put("key", "value");
for (Main.Entry e : m.entrySet())
String s = e.getValue(); // this line throws an error telling that getValue() returns Object and not String
}
}
【问题讨论】:
-
因为您声明了原始类型的
e变量。省略类型参数并不意味着“嘿javac,请推断类型参数”。