解决Groovy Map类 [英] Resolving Groovy Map class
问题描述
任何人都可以解释为什么在地图上调用[:].class返回null,而在调用[:].getClass()时却返回预期结果Map的原因.参见下面的示例
Anyone can explain why calling [:].class on a map return null while calling [:].getClass() return the expected result Map. See example below
1-
["test",[test:"test"],23].each {
println it.class
}
class java.lang.String
null
class java.lang.Integer
2-
["test",[test:"test"],23].each {
println it.getClass()
}
class java.lang.String
class java.util.LinkedHashMap
class java.lang.Integer
肯
推荐答案
这是答案
http://jira.codehaus.org/browse/GROOVY-1824
编辑-可以.我认为Map的语义就是这样,如果您有
EDIT -- sure. I think the semantics of a Map are such that if you have
def m = [one:1, two:2]
您应该能够像这样访问地图中的条目
you are supposed to be able to access the entries in the map like
m.one
换句话说,访问地图就像在地图对象上获取属性一样.如果
in other words, access into the map is like getting a property on the map object. If
m.class
返回类,它将破坏这些语义,因为类"不是程序员添加到映射中的键.
returned the class, it would break those semantics, because 'class' is not a key added to the map by the programmer.
那是我收集的....
这篇关于解决Groovy Map类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!