预定义的 groovy 变量列表

发布于 2024-12-06 03:08:15 字数 583 浏览 0 评论 0原文

我是 groovy 的新手,我想知道在哪里可以找到预定义的完整列表
itdelegate 这样的常规变量?

我特别感兴趣的是是否有预定义的关键字
对调用当前方法的对象的引用,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-12-13 03:08:15

对于所有实际关键字(比您想象的要少)和类似关键字的对象级属性的良好列表,这篇文章非常好:http://marxsoftware.blogspot.com/2011/09/groovys-special-words.html

如果您有控制示例中的 doJob 方法,那么您应该设置闭包的委托:

def doJob(Closure closure) {
    closure.delegate = this
    closure.resolveStrategy = Closure.DELEGATE_FIRST
    // loop or whatever
    closure()
}

现在,在闭包中,您可以直接引用父对象上的任何属性,像这样:

myObject.doJob ({ 
    ...
    getField()
    ...
})

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 the delegate of the closure:

def doJob(Closure closure) {
    closure.delegate = this
    closure.resolveStrategy = Closure.DELEGATE_FIRST
    // loop or whatever
    closure()
}

Now, in your closure, you can reference any properties on the parent object directly, like so:

myObject.doJob ({ 
    ...
    getField()
    ...
})

Groovy Closures - Implicit Variables.

素手挽清风 2024-12-13 03:08:15

你要这个吗?

int number = 5
number.times { print number - it }

希望这会帮助你

Are you asking for this?

int number = 5
number.times { print number - it }

Hope this will help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文