
问题在于您的代码仅指定映射中的值为
Object。您不仅仅知道这些,所以请告诉编译器以下信息:
HashMap<String, InfoStor> mapper = new HashMap<String, InfoStor>();mapper.put("NS01", new InfoStor("NS01"));...InfoStor value = mapper.get("NS01");Integer memory = value.getMemory();请注意,对于变量类型使用接口 通常 并不总是更好-并且您可以对构造函数调用使用菱形运算符,让编译器使用类型推断来填充类型参数:
Map<String, InfoStor> mapper = new HashMap<>();mapper.put("NS01", new InfoStor("NS01"));...InfoStor value = mapper.get("NS01");Integer memory = value.getMemory();欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)