這是行不通的。不應該嗎?Groovy擴展hashmap與字段
class WeirdBean extends HashMap {
public String inner = "set within"
def getInner() { return this.inner }
def getOuter() { return this.outer }
}
def o = WeirdBean.newInstance()
o.outer = "set without"
println o.getOuter() // set without
println o.outer // set without
assert o.outer == o.getOuter() // Pass
println o.getInner() // set within
println o.inner // null, even though public
assert o.inner == o.getInner() // Fail, o.inner is null
要直接訪問字段,你必須使用'o。@內部'否則'o.inner'將被視爲獲取鍵'這裏明顯是'null' – dmahapatro