- 源代码:
- package mars.groovy
- /**
- * @author Eric Han
- * 2008-9-3 11:58:28
- */
- public class MyClosure{
- public static void main(def args){
- def myfirstClosure={man->println "Hello ${man}"}
- myfirstClosure.call('Eric')
- myfirstClosure('Carry')
- println ''
- def map=['Eric':30,'bobo':29,'Carry':25]
- map.each{println "${it.key} age is: ${it.value}"}
- println ''
- map.findAll{age->age.value>28}.each{println it}
- println ''
- def isAnyOneTrue=[11,12,13,14].any{n->n>12}
- def isEveryOneTrue=[11,14].any{n->n>10}
- println "any one is true? ${isAnyOneTrue}"
- println "every one is true? ${isEveryOneTrue}"
- def result
- println ''
- def list=[1,2,3,4,5]
- result=list.collect{n->return n*n}
- println "collect result:${result}"
- println ''
- def list1=[1,5]
- result=list1.inject(1){prevIoUsValue,currentValue->prevIoUsValue*currentValue}
- println "inject result:${result}"
- }
- }
- 输出:
- Hello Eric
- Hello Carry
- Eric age is: 30
- bobo age is: 29
- Carry age is: 25
- bobo=29
- Eric=30
- any one is true? true
- every one is true? true
- collect result:[1, 4, 9, 16, 25]
- inject result:120
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。