groovy in action 笔记 2

这里看的时间稍微长了一点,从43到100页,看起来还算顺。

首先是看到一个spaceship operator: <=>,作为少数dynamic language拥有的特殊比较符号,可以认为是以下函数

//a<=>b
if(a>b){return 1;}
else if(a==b){return 0;}
else if(a<b){return -1}
else {return undef;}


第二个是duck typing。xy并没有完全理解这个词组的意思,按照WIKIPEDIA的解释:

duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

而duck typing的由来是duck testing,也就是“某个学鸭子走路,鸭子叫的鸟,我们认为是鸭子”的实验。

就XY的理解,方法体 a*3, 假如a是int,则返回数学意义上的乘法,但是如果我输入‘hello',则返回'hellohellohello',也就是连接起来的3个'hello'。有人认为这种方法需要写文档,提示programmers。

另外一方面,这个duck typing是static 和dynamic language的不同的地方,作为从static过渡到dynamic的,需要学习这个duck typing来发挥dynamic的优势。

第三个是double dispatch,WIKIPEDIA的解释:
double dispatch is a mechanism that dispatches a function call to different concrete functions depending on the runtime types of multiple objects involved in the call.

XY的理解基于1+1.0,按照dynamic language的设定,这个1是Integer,而这个1.0是BigDecimal,假如我在Integer的类中写上plus(BigDecimal ???)的语句的话,我可以调用BigDecimal的plus(Integer ???)的方法。事实上1+1.0==1.0+1,而我确实可以通过这个double dispatch来reuse。不过个人认为这样做有一个前提,就是parameters必须是object,否则double dispatch会有限制。这个问题在dynamic language上基本不会出现。

第四个是1+1的问题,也就是类型问题,因为java中除了String之外,Object和Object不能相加,只有primitive类型才可以操作。但是在dynamic language中,这个限制被取消了,这里的两个1都是Integer,调用plus方法,返回一个值为2的Integer。

虽然这个1+1问题是在autoboxing的时候提到的,但是,只要不接触java代码,那么就没有autoboxing的步骤。

最后是让xy可能一生难忘的“一行遍历当前目录下所有文件/文件夹“的代码。

new File('.).eachFileRecurse{println it}


这句话中有很多可以分析的要点。
1:'',这个是单引号,而不是双引号。按照groovy的设定,单引号内容不解析,这个是需要了解的。
2:new File('.'),匿名类的实例+构造函数,这个和java区别不大。
3:eachFileRecurse,递归遍历文件,可以附加一个closure
4:{println it}:,losure,默认的参数为it,这个要记住
5:println it,it为一个java.io.File的实例,知道这个之后我们可以把it替换为it.size()来遍历显示文件大小(包括文件夹);其次println调用了it的toString()方法。

结语:再次被dynamic language的魅力征服
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Groovy in Action, Second Edition is the undisputed definitive reference on the Groovy language. Written by core members of the Groovy language team, this book presents Groovy like no other can—from the inside out. With relevant examples, careful explanations of Groovy's key concepts and features, and insightful coverage of how to use Groovy in-production tasks, including building new applications, integration with existing code, and DSL development, this is the only book you'll need. Updated for Groovy 2.4. Some experience with Java or another programming language is helpful. No Groovy experience is assumed. Table of Contents Part 1 The Groovy language Chapter 1 Your way to Groovy Chapter 2 Overture: Groovy basics Chapter 3 Simple Groovy datatypes Chapter 4 Collective Groovy datatypes Chapter 5 Working with closures Chapter 6 Groovy control structures Chapter 7 Object orientation, Groovy style Chapter 8 Dynamic programming with Groovy Chapter 9 Compile-time metaprogramming and AST transformations Chapter 10 Groovy as a static language Part 2 Around the Groovy library Chapter 11 Working with builders Chapter 12 Working with the GDK Chapter 13 Database programming with Groovy Chapter 14 Working with XML and JSON Chapter 15 Interacting with Web Services Chapter 16 Integrating Groovy Part 3 Applied Groovy Chapter 17 Unit testing with Groovy Chapter 18 Concurrent Groovy with GPars Chapter 19 Domain-specific languages Chapter 20 The Groovy ecosystem Appendix A Installation and documentation Appendix B Groovy language information Appendix C GDK API quick reference Appendix D Cheat sheets Appendix E Annotation parameters Appendix F Compiler phases Appendix G AST visitors Appendix H Type checking extensions Appendix I Android support

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值