- 浏览: 42810 次
- 性别:
- 来自: 上海
最新评论
-
wangdgsc:
你好,我最近也在搞这个strutstest,不过遇到了一个问题 ...
使用cactus测试ssh应用(2) -
gsshcl:
(=ω=)哼哼~果然是人老了,看不清了啊~~~~
ubuntu修改tty分辨率
这里看的时间稍微长了一点,从43到100页,看起来还算顺。
首先是看到一个spaceship operator: <=>,作为少数dynamic language拥有的特殊比较符号,可以认为是以下函数
第二个是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可能一生难忘的“一行遍历当前目录下所有文件/文件夹“的代码。
这句话中有很多可以分析的要点。
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的魅力征服
首先是看到一个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的魅力征服
发表评论
-
groovy console readInt
2009-08-03 09:24 1485手中有一本groovy的教程,是那本不算厚的来自某个外国大学的 ... -
linux下用aptana“代替”dreamwaver
2009-07-29 09:25 1520好久没有来这里写东西,主要还是在twitter和自己学校论坛活 ... -
jspwiki+glassfish(zip)
2009-07-04 06:01 1114使用glassfish的zip包原因是要模拟在服务器上配置gl ... -
随想0702
2009-07-02 21:21 627早上首先考虑如何替换glassfish的admin的密码,监视 ... -
初识glassfish
2009-07-01 18:59 863以前XY一直使用的是apache+tomcat的配置。tomc ... -
ubuntu9.04上编译安装firefox 3.5(shiretoko)
2009-07-01 09:55 3530昨天,也就是6月30日,firefox 3.5正式发布了,不过 ... -
S60手机使用putty进行ssh登录
2009-06-30 08:03 2170在同学的介绍下,了解到putty居然有s60版本。正好xy的手 ... -
使用抓虾
2009-06-22 07:28 706最近寻找RSS的时候,发现了一个不错的网站,抓虾。 本来XY是 ... -
EBVIEW和DICTCN
2009-06-05 13:48 1011EBVIEW EBVIEW是一个不错的日文字典,在gnome- ... -
vsftpd安装与配置
2009-06-05 12:55 921以下主要是作为备忘。 从ftp://vsftpd.beast ... -
解决了不小心删除synaptic的问题
2009-06-05 04:12 874首先通过 sudo apt-get install synap ... -
重新安装电脑
2009-06-05 03:00 720这次相比几个星期以前的重装不同在于确定了基本上不使用windo ... -
哎,果然还是无法和C++断绝关系阿
2009-05-30 20:28 744XY不喜欢C++,是有原因的.不过XY不想去回忆自己为什么会讨 ... -
Firefox,网站开发第二个IDE
2009-05-30 19:17 857取这个标题,并不是为了 ... -
现在要找一本适合的书难啊
2009-05-30 13:54 695理论上来说,现在的书越来越多,应该更加容易找到适合自己的书才对 ... -
使用cactus测试ssh应用(2)
2009-02-21 19:54 1395接1 接下来就是考虑如 ... -
使用cactus测试ssh应用(1)
2009-02-21 19:23 2263最近拿到了《Junit In Acti ... -
尝试使用edtftpj遍历FTP服务器上的列表
2008-12-13 16:09 1958edtFTPj提供了一个简易的构建FTP客户端的方法集合。现在 ...
相关推荐
groovy in action 中文版 2017.11 groovy in action 中文版 2017.11
groovy in action中文版,带标签
Groovy in Action的源代码。Groovy in Action的源代码。
Groovy in Action Groovy实战 英文第2版 Groovy in Action Groovy实战 英文第2版
Groovy in Action[文字版][中文] Groovy in Action[文字版][中文]
groovy in action 详细语法讲解,附例子程序, ofbiz 学习必备。
Groovy in Action, Second Edition is a thoroughly revised, comprehensive guide to Groovy programming. It introduces Java developers to the dynamic features that Groovy provides, and shows how to apply ...
Groovy IN ACTION
Groovy in action 中英文,API
It’s only natural that many readers of the first edition of Groovy in Action (or “Gina” as we say for short) demanded an update that we are now happy to deliver as the second edition (codename ...
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 ...
《Groovy in Action》是Groovy编程的综合指南,它向Java开发者介绍了Groovy提供的新的动态特性。为了呈现《Groovy in Action》,Manning再次从源头工作,与包括Groovy项目团队成员和经理在内的专家作者团队合作。其结果...
Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the...
这是我自己翻译的,目前还只有第一部分,后续工作还在继续
groovy in action, 2nd, for unit test writing and gradle DSL
Groovy是一种语言,其语法类似于Java,但比Java更简单。它通常被视为脚本/灵活/动态的语言,但是我不喜欢这类形容词,因为我认为它们只会令人困惑。如果说Java是一位明智的中年男子,那么Groovy就是他十几岁的儿子。...
Groovy in Action.pdf
Groovy in Action 配套源码
groovy in action.pdf