我写了一个小 Groovy 脚本,它暴露了一个非常奇怪的行为。谁能解释一下?
// Creating a groovy map
def map = [:]
// Putting a value in
map["a"]="b"
// Render it without trouble
println map["a"]
// Putting another value in (yup, this one has THE name)
map["metaClass"]="c"
// Failing to render it
println map["metaClass"]
在这种情况下,我的问题很简单:为什么最后一条指令会引发以下异常:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'c' with class 'java.lang.String' to class 'groovy.lang.MetaClass'
at Script1.run(Script1.groovy:8)