我只是在玩 Groovy 中的元类编程。但是突然间我遇到了一个小问题,我无法开始工作......
这是简单的脚本:
// define simple closure
def printValueClosure = {
println "The value is: '$delegate'"
}
String.metaClass.printValueClosure = printValueClosure
// works fine
'variable A'.printValueClosure()
// define as method
def printValueMethod(String s){
println "The value is: '$s'"
}
// how to do this!?
String.metaClass.printValueMethod = this.&printValueMethod(delegate)
'variable B'.printValueMethod()
是否可以使用该方法但将第一个参数设置为调用对象?使用委托似乎不起作用......不引用调用者的方法的分配没有问题。咖喱在这里有用吗?
谢谢,英戈