【发布时间】:2015-06-10 21:03:53
【问题描述】:
我第一次使用@CompileStatic,对 Groovy 的地图构造函数在这种情况下的工作方式感到困惑。
@CompileStatic
class SomeClass {
Long id
String name
public static void main(String[] args) {
Map map = new HashMap()
map.put("id", 123L)
map.put("name", "test file")
SomeClass someClass1 = new SomeClass(map) // Does not work
SomeClass someClass2 = map as SomeClass // Works
}
}
鉴于上面的代码,我在尝试编译时看到以下错误
Groovyc: Target constructor for constructor call expression hasn't been set
如果@CompileStatic 被删除,两个构造函数都可以正常工作。
谁能解释为什么new SomeClass(map) 不能与@CompileStatic 一起编译?还有一个可能的补充,为什么map as SomeClass 仍然有效?
【问题讨论】:
标签: groovy