Practice in groovy
来源:互联网 发布:上古网络用语 编辑:程序博客网 时间:2024/06/09 16:50
2015年国庆过后,我所实习的公司DP与MT合并了,当时也是爆炸新闻,从相爱到相亲….着实让我们感动的内流满面。最主要的,在这个buffer空档期,原有的planning基本都做了delay,这也造就了公司Team间的技术大讨论,形成了难得的交流分享浪潮。
当时我们Team瞄准了新的语言groovy,它与java有异曲同工之处,而且对于java狗出身的我们来说,groovy学习曲线很快,能无缝的融合java代码。当时老大让我来试水重构了当时的一个项目模块,效果惊人,我记得当时LOC为600重构后不到200,这也让我屡试不爽。
下面主要分享下groovy的使用心得:
- Groovy引入:由于我们的项目使用pom依赖,因此使用groovy只需要引入相关的依赖即可。
//groovy-all为groovy依赖组件名,spock-core是测试使用到到依赖组件名 <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>0.7-groovy-2.0</version> <exclusions> <exclusion> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> </exclusion> </exclusions> </dependency> <plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>
2.声明变量
可使用def关键字声明任何类型
不需要加分号;
默认生成setter和getter方法;
groovy 中变量不存在简单类型,万事万物皆对象,即使使用int声明也会转化成Integer
Integer selectedDealGroupId Integer selectedDealId DealGroupInfoBean selectedDealGroupInfo String selectedBeginDate = ""
3.结构化方式判断是否为空
if (isFirstDay || !hasOverLapDate)
4.初始化
//相当于调用set方法 new AClass(attribute1:value1,attribute2:value2 ...); //访问成员变量可以直接使用.,相当于调用get方法 a.attribute //集合的初始化方式 def List a = [] def map = [:] a << 1
5.闭包的使用(groovy的强大特性)
//类似于for...each...,{}是具体的处理逻辑 list.each { dealIds << it.dealId //集合的排序 finalDealSaleDTOList.sort { e1, e2 -> return Date.parse("yyyy-MM-dd", e1.calDate) - Date.parse("yyyy-MM-dd", e2.calDate) } //生成新的集合 def dates = item.collect { Date.parse("yyyy-MM-dd", string) }
6.时间的使用
//Date类型的时间集合声明,..指定时间段,-可作相减,还可进行大小的比较 def allDate = (beginDate..endDate) def differDate = allDate - saleDates date-1 //日期减一天 //Date和String之间的转换 string = date.format("yyyy-MM-dd") Date.parse("yyyy-MM-dd", string) //补注:java 中Date和String的转换 DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd"); Date date = fmt.parse(string); string = fmt.format(date);
以上只是本人使用groovy的简单总结,当然还有很多特性需要我们更多的去实践。
0 0
- Practice in groovy
- Groovy in Action
- each() in Groovy
- Groovy in SOAP UI
- Groovy in SOAP UI
- groovy in action
- UT in groovy
- HttpSessionBindingListener in practice
- practice in C
- PHP 5 in Practice
- PHP 5 in Practice
- Java Concurrency in Practice
- Ajax in Practice
- GWT in Practice
- Practice in ES
- JBehave in Practice
- Best Practice in SQL
- Inverted Index in practice
- Redis2.8.9初识(-)
- std::make_shared解释。。。。
- 连续属性离散化
- MySQL创建、修改表、表的约束
- 163.You want to access employee details contained in flat files as part of the EMPLOYEE table. You p
- Practice in groovy
- qsort函数的应用
- PL/SQL Server 的tnsname.ora注意事项
- c语言的关键字
- vs2015 企业版 专业版 密钥
- win10除了Edge浏览器外其他浏览器不能上网的解决办法
- JavaScript 字符串处理详解
- 顺序表基本操作
- C++ 类中不同类型成员函数的定义