【发布时间】:2011-10-31 03:03:23
【问题描述】:
我是 groovy 的新手,我正在阅读一个项目的源代码gretty
import org.codehaus.jackson.map.ObjectMapper
class JacksonCategory {
static final ObjectMapper mapper = []
...
}
我看不懂代码ObjectMapper mapper = [],这里[]是什么意思?我以为是list,但是如何将其分配给ObjectMapper?
更新
取决于Dunes's answer,似乎[] 表示invocation of default constructor。所以,这意味着:
static final ObjectMapper mapper = new ObjectMapper()
但是:
String s = []
println s // -> it's `[]` not ``
和
Integer i = []
抛出异常:
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]'
with class 'java.util.ArrayList' to class 'java.lang.Integer'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]' with class
'java.util.ArrayList' to class 'java.lang.Integer'
【问题讨论】: