有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

泛型Java6映射。get()类型安全性意外行为(?)

Possible Duplicate:
What are the reasons why Map.get(Object key) is not (fully) generic

根据映射接口的javadocs(http://java.sun.com/javase/6/docs/api/java/util/Map.html),get的定义是

V get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

代码示例:

Map<InstrumentInfo, Double> moo = new HashMap<InstrumentInfo,Double>();
moo.get(new Integer(5));

我希望上面的代码会抛出一个异常,或者至少给出一个警告

我希望在使用泛型和类型安全性的情况下,get方法将接受类型为的参数。接受类型对象而不接受类型对象的原因是什么


共 (1) 个答案