Dan*_*ton 3 groovy metaprogramming metaclass
在Groovy中,我可以通过猴子修补元类的call方法使对象像函数一样调用:
myObject.metaClass.call = { "hello world" }
println myObject() // prints "hello world"
Run Code Online (Sandbox Code Playgroud)
修补call只允许我调用没有参数的对象.有没有办法允许使用标准函数式语法使用参数调用对象?
编辑:一个答案正如tim_yates所暗示的那样,尽管值得注意的是ataylor的评论,你可以简单地覆盖call而不用明确的元编程:
class MyType {
def call(...args) {
"args were: $args"
}
}
def myObject = new MyType()
println myObject("foo", "bar") // prints 'args were ["foo", "bar"]'
Run Code Online (Sandbox Code Playgroud)
显然,技巧是使用的可变参数签名...args.
你可以这样做:
myObject.metaClass.call = { ...args -> "hello $args" }
assert myObject( 'one', 'two', 'three' ) == 'hello [one, two, three]'
Run Code Online (Sandbox Code Playgroud)
(如你所见,args是一个对象数组)
或者对于一个参数:
myObject.metaClass.call = { who -> "hello $who" }
Run Code Online (Sandbox Code Playgroud)
或者,如果您希望将该单个参数作为可选参数,则可以执行以下操作:
myObject.metaClass.call = { who = null -> "hello ${who ?: 'world'}" }
assert myObject( 'tim' ) == 'hello tim'
assert myObject() == 'hello world'
Run Code Online (Sandbox Code Playgroud)