本文介绍了为什么Groovy的地图没有metaClass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么Groovy的字面映射没有metaClass?
Why does Groovy's literal map does not have a metaClass?
// lists work as expected: aList = [] println aList.class // class java.util.ArrayList println aList.metaClass // gives the full blown metaclass // org.codehaus.groovy.runtime.HandleMetaClass@3de6696c // [groovy.lang.MetaClassImpl@3de6696c[class java.util.ArrayList]] // string and numbers too: println ''.metaClass println 12.metaClass // map does not: aMap = [:] println myMap.metaClass // gives null println myMap.class // also gives null
测试结果:
Tested with:
Groovy Version: 1.8.6 JVM: 1.6.0_31 Vendor: Sun Microsystems Inc. OS: Linux
推荐答案
您必须使用:
You have to use:
[:].getMetaClass()
就像您想获得类一样 Map ,即:
The same as if you want to get the class of a Map, ie:
[:].getClass()
这是因为当您执行属性时,Maps会返回给定键表示的值在他们身上查找。否则,你不能有键 class 或 metaClass
this is because Maps return the value represented by the given key when you do a property lookup on them. Otherwise you couldn't have keys class or metaClass
这篇关于为什么Groovy的地图没有metaClass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!