Object 객체에 대한 GDK 확장

2024 단어 groovy

Object 객체에 대한 GDK 확장


Groovy는 java.lang.Object 에 몇 가지 방법을 추가했는데, 그 중 대부분은 목록이나 DOM 노드와 같은 집합 또는 집합으로 처리된다.
Return Value
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가 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

좋은 웹페이지 즐겨찾기