Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了GDK对Object对象的扩展大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

GDK对Object对象的扩展

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

Return Value

@H_882_6@method

Description

Boolean

any {ClosurE}

returns true if the closure returns true for any item

List

collect {ClosurE}

returns a list of all items that were returned from the closure

Collection

collect(Collection collection) {ClosurE}

same as above,but adds each item to the given collection

void

each {ClosurE}

simply executes the closure for each item

void

eachWithIndex {ClosurE}

same as each{} except it passes two arguments: the item and the index

Boolean

every {ClosurE}

returns true if the closure returns true for all items

Object

find {ClosurE}

returns the first item that matches the closure expression

List

findAll {ClosurE}

returns all items that match the closure expression

Integer

findIndexOf {ClosurE}

returns the index of the first item that matched the given expression

**注释**以上表格只列出部分常用的方法添加到`java.lang.object`中的完整方法列表请参阅[GDK documentation on Object ](http://groovy.codehaus.org/groovy-jdk/java/lang/Object.html) 在Groovy当中,自从`return`关键字变成可选之后,闭包在这种情况下被当做`判断`,并且在闭包当中你无论给出的是什么表达式,返回的结果都是布尔型的。这种`判断`允许你以一种非常简洁的方式在集合对象上执行操作。 ### 例子
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

原文地址 http://groovy.codehaus.org/GDK+Extensions+to+Object
@H_674_178@

大佬总结

以上是大佬教程为你收集整理的GDK对Object对象的扩展全部内容,希望文章能够帮你解决GDK对Object对象的扩展所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。