您的位置:首页 > 移动开发 > Objective-C

GDK对Object对象的扩展

2015-02-18 00:00 246 查看
摘要: 刚开始学Groovy,看别人的代码时老是有很多这样的那样的方法,都不知道哪里来的,看了官方的这篇文章帮我解决了不少疑惑,翻译过来记录下。

GDK对Object对象的扩展

Groovy添加了若干方法到
java.lang.Object
当中,其中大部分处理类型作为集合或聚合,如列表或DOM节点。

<table class="confluenceTable"><tbody><tr><th class="confluenceTh"><p>Return Value</p></th><th class="confluenceTh"><p>Method</p></th><th class="confluenceTh"><p>Description</p></th></tr><tr><td class="confluenceTd"><p>Boolean</p></td><td class="confluenceTd"><p>any {closure}</p></td><td class="confluenceTd"><p>returns <code>true</code> if the closure returns true for any item</p></td></tr><tr><td class="confluenceTd"><p>List</p></td><td class="confluenceTd"><p>collect {closure}</p></td><td class="confluenceTd"><p>returns a list of all items that were returned from the closure</p></td></tr><tr><td class="confluenceTd"><p>Collection</p></td><td class="confluenceTd"><p>collect(Collection collection) {closure}</p></td><td class="confluenceTd"><p>same as above, but adds each item to the given collection</p></td></tr><tr><td class="confluenceTd"><p>void</p></td><td class="confluenceTd"><p>each {closure}</p></td><td class="confluenceTd"><p>simply executes the closure for each item</p></td></tr><tr><td class="confluenceTd"><p>void</p></td><td class="confluenceTd"><p>eachWithIndex {closure}</p></td><td class="confluenceTd"><p>same as each{} except it passes two arguments: the item and the index</p></td></tr><tr><td class="confluenceTd"><p>Boolean</p></td><td class="confluenceTd"><p>every {closure}</p></td><td class="confluenceTd"><p>returns <code>true</code> if the closure returns <code>true</code> for <em>all</em> items</p></td></tr><tr><td class="confluenceTd"><p>Object</p></td><td class="confluenceTd"><p>find {closure}</p></td><td class="confluenceTd"><p>returns the first item that matches the closure expression</p></td></tr><tr><td class="confluenceTd"><p>List</p></td><td class="confluenceTd"><p>findAll {closure}</p></td><td class="confluenceTd"><p>returns all items that match the closure expression</p></td></tr><tr><td class="confluenceTd"><p>Integer</p></td><td class="confluenceTd"><p>findIndexOf {closure}</p></td><td class="confluenceTd"><p>returns the index of the first item that matched the given expression</p></td></tr></tbody></table>
**注释**以上表格只列出部分常用的方法,添加到`java.lang.Object`中的完整方法列表请参阅[GDK documentation on Object ](http://groovy.codehaus.org/groovy-jdk/java/lang/Object.html)
在Groovy当中,自从`return`关键字变成可选之后,闭包在这种情况下被当做`判断`,并且在闭包当中你无论给出的是什么表达式,返回的结果都是布尔型的。这种`判断`允许你以一种非常简洁的方式在集合对象上执行操作。
### 例子
<pre class="brush:groovy">
def numbers = [ 5, 7, 9, 12 ]
assert numbers.any { it % 2 == 0 } //returns true since 12 is even

assert numbers.every { it > 4 } //returns true since all #s are > 4

assert numbers.findAll { it in 6..10 } == [7,9] //returns all #s > 5 and < 11

assert numbers.collect { ++it } == [6, 8, 10, 13] //returns a new list with each # incremented

numbers.eachWithIndex{ num, idx -> println "$idx: $num" } //prints each index and number
</pre>
原文地址 http://groovy.codehaus.org/GDK+Extensions+to+Object
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  groovy
相关文章推荐