GDK对Object对象的扩展
2015-02-18 00:00
246 查看
摘要: 刚开始学Groovy,看别人的代码时老是有很多这样的那样的方法,都不知道哪里来的,看了官方的这篇文章帮我解决了不少疑惑,翻译过来记录下。
<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
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 Tip 11 GDK的Object扩展方法
- C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- ES6学习——新的语法:对象字面量扩展(Object Literal Extensions)
- ES6-对象的扩展-Object.is()
- ES6之对象字面量扩展语法(Enhanced Object Literals)
- Javascript对象扩展 - Object
- ES6-对象的扩展-Object.keys(),Object.values(),Object.entries()
- ES5 对象的扩展(Object.preventExtensions)、密封(Object.seal)和冻结(Object.freeze)
- ES6学习——新的语法:对象字面量扩展(Object Literal Extensions)
- ES6-对象的扩展-Object.getOwnPropertyDescriptors()
- [iphone 开发学习笔记]Object-C和C语言最大的区别也是最大的扩展-----消息(即如何调用一个对象中的函数)
- 扩展juery 将 js object对象 转成 json结构字符串 用于ajax 传递json (验证可用)后台 对应fastJSON
- VS 2008 + .NET 3.5 - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- ECMAScript6(ES6)标准之对象Object扩展方法及新特性
- jQuery.extend( object ); 扩展jQuery对象本身
- 3、步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- Object、Function、String、Array原生对象扩展方法
- jQuery 工具类函数-使用$.extend()扩展Object对象
- 从零开始学_JavaScript_系列(39)——对象的扩展(2)Object对象的扩展方法
- ES6-对象的扩展-_proto_ 属性,Object.setPrototypeOf(),Object.getProtitypeOf()