我有以下关闭
def closure = { println ("closure code") }
我想为它添加一个方法.但如果我试试
closure.metaClass.fun = { c-> c.call(); println ("extra code"); }
我得到一个例外
groovy.lang.MissingPropertyException: No such property: fun for class: org.codehaus.groovy.runtime.metaclass.ClosureMetaClass
读另一个答案,我也盲目地试着打电话
ExpandoMetaClass.enableGlobally()
但它不起作用.
有没有办法实现我想要的东西?
你可以做:
def closure = { println "closure code" } closure.getMetaClass().fun = { -> delegate.call() println "extra code" } closure.fun()
哪个印刷品:
closure code extra code