预定义的 groovy 变量列表
我是 groovy 的新手,我想知道在哪里可以找到预定义的完整列表
像 it 和 delegate 这样的常规变量?
我特别感兴趣的是是否有预定义的关键字
对调用当前方法的对象的引用,例如:
5.times { print 5 - it}
使用 such 关键字,它应该类似于:
5.times { print *keyword* - it }
所以问题是应该使用什么关键字?
PS:另一个例子:
MyObject myObject = new myObject();
myObject.getField(); // MyObject has method named getField
myObject.doJob ({
...
((MyObject)*keyword*).getField(); // instead of myObject.getField();
...
})
I'm new to groovy and I'm wondering where can I find a full list of predefined
groovy variables like it and delegate?
The particular thing that I'm interested in is if there are predefined keyword for
the reference to the object from where the current method was invoked, for example:
5.times { print 5 - it}
with the use of such keyword it should be something like:
5.times { print *keyword* - it }
so the question is what's the keyword should be used there?
P.S.: another example:
MyObject myObject = new myObject();
myObject.getField(); // MyObject has method named getField
myObject.doJob ({
...
((MyObject)*keyword*).getField(); // instead of myObject.getField();
...
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于所有实际关键字(比您想象的要少)和类似关键字的对象级属性的良好列表,这篇文章非常好:http://marxsoftware.blogspot.com/2011/09/groovys-special-words.html
如果您有控制示例中的 doJob 方法,那么您应该设置闭包的委托:
现在,在闭包中,您可以直接引用父对象上的任何属性,像这样:
Groovy 闭包 - 隐式变量。
For a good list of all actual keywords (which are fewer than you'd think) and object-level properties that are like keywords, this article is really good: http://marxsoftware.blogspot.com/2011/09/groovys-special-words.html
If you have control over the
doJob
method in your example, then you should set thedelegate
of the closure:Now, in your closure, you can reference any properties on the parent object directly, like so:
Groovy Closures - Implicit Variables.
你要这个吗?
希望这会帮助你
Are you asking for this?
Hope this will help you