0
亚型我有一张地图,声明如下:铸造斯卡拉
private var iCacheMap: HashMap[Class[_ <: ICacheable], String] = ....
因此,对于这个iCacheMap的关键是实现ICacheable接口
我再想找查询该地图的一类,如下:
private def queryICacheMap(message: AnyRef) {
val iCacheable = message.asInstanceOf[ICacheable]
val myString = iCacheMap.get(classOf[iCacheable]).get
// ...do something with myString
}
不过,我得到一个类型不匹配异常解释说,我们期待A类_ <:ICacheable],但实际是类[任何]
我需要做什么才能正确投射这些东西?
不知道我遵循,在我上面的例子中的编译错误是为classOf [iCacheable。你能展示上述答案如何解决这个问题吗?谢谢 – DJ180
我改进了这个例子。你必须使用getClass。 – Marco