加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy的闭包示例

发布时间:2020-12-14 16:59:00 所属栏目:大数据 来源:网络整理
导读:?源代码: 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' ) ??my
  1. ?源代码:
  2. package?mars.groovy
  3. /**
  4. ?*?@author?Eric?Han
  5. ?*?2008-9-3?11:58:28
  6. ?*/
  7. public?class?MyClosure{
  8. ?public?static?void?main(def?args){
  9. ??def?myfirstClosure={man->println?"Hello?${man}"}
  10. ??
  11. ??myfirstClosure.call('Eric')
  12. ??myfirstClosure('Carry')
  13. ??
  14. ??println?''
  15. ??def?map=['Eric':30,'bobo':29,'Carry':25]
  16. ??map.each{println?"${it.key}?age?is:?${it.value}"}
  17. ??
  18. ??println?''
  19. ??map.findAll{age->age.value>28}.each{println?it}
  20. ??
  21. ??println?''
  22. ??def?isAnyOneTrue=[11,12,13,14].any{n->n>12}
  23. ??def?isEveryOneTrue=[11,14].any{n->n>10}
  24. ??println?"any?one?is?true??${isAnyOneTrue}"
  25. ??println?"every?one?is?true??${isEveryOneTrue}"
  26. ???
  27. ??def?result
  28. ??println?''
  29. ??def?list=[1,2,3,4,5]
  30. ??result=list.collect{n->return?n*n}
  31. ??println?"collect?result:${result}"
  32. ??
  33. ??println?''
  34. ??def?list1=[1,5]
  35. ??result=list1.inject(1){previousValue,currentValue->previousValue*currentValue}
  36. ??println?"inject?result:${result}"
  37. ?}
  38. }
  39. 输出:
  40. Hello?Eric
  41. Hello?Carry
  42. Eric?age?is:?30
  43. bobo?age?is:?29
  44. Carry?age?is:?25
  45. bobo=29
  46. Eric=30
  47. any?one?is?true??true
  48. every?one?is?true??true
  49. collect?result:[1,?4,?9,?16,?25]
  50. inject?result:120

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读