2
Groovy的版本:2.4.3 JVM:1.8.0_60供應商:Oracle公司操作系統:Mac OS X發起授權領域成爲一個封閉空,Groovy的
import groovy.transform.*
@Canonical
class A {
def f() {
map = [:] //map is not null
3.times {
assert map != null // failed, map is null?!
}
}
@Delegate
Map map
}
new A().f()
當我打電話F(),我得到了一個斷言失敗,這意味着映射爲空。但是,如果我刪除註釋'@Delegate',那麼不會有任何問題。或者,如果斷言不在閉包中,也沒有問題。我的問題是爲什麼委託字段在關閉中或不在關閉中表現不同?如果這是因爲閉包中的映射與A類中的對象不是同一個對象,爲什麼它在刪除註釋後工作?
import groovy.transform.*
@Canonical
class A {
def f() {
map = [:]
3.times {
assert map != null // No problem, map is not null
}
}
Map map
}
new A().f()
或者
import groovy.transform.*
@Canonical
class A {
def f() {
map = [:]
assert map != null //no problem too
}
@Delegate
Map map
}
new A().f()