1、杂学
package groovy
class Foo {
def static openFile(fileName) {
new FileInputStream(fileName)
}
def static foo(str) {
str?.reverse()
}
def static returnWhat() {
println "returnWhat() executed!"
this
}
public static void main(String[] args) {
/*
* 无需返回申明,自动返回最后出现的变量(条件语句中会返回空)
* 方法可以链式执行
*/
println returnWhat().returnWhat().returnWhat()
/* openFile方法不强制你抛出异常 */
try {
openFile("nonexistentfile" )
} catch(FileNotFoundException ex) {
println ex
}
/* 安全导航操作符?.只在对象不为空时调用 */
println foo('evil' )
println foo(null)
/* 闭包、循环方式 */
List lst = [1, 2, 3]
lst.each { print "$it " }
print "\n"
for(j in 5..9) {
print j + " "
}
print "\n"
10.upto 15, { print "$it " }
print "\n"
3.times { print "--\n" }
0.step(10, 2) { print "$it " }
/* 直接调用系统命令 */
print "\n"
println "java -version".execute().text
/* 操作符重载 */
lst<<4
println lst
}
}
输出:
returnWhat() executed!
returnWhat() executed!
returnWhat() executed!
class groovy.Foo
java.io.FileNotFoundException: nonexistentfile (系统找不到指定的文件。)
live
null
1 2 3
5 6 7 8 9
10 11 12 13 14 15
--
--
--
0 2 4 6 8
[1, 2, 3, 4]
二、对象与线程
package groovy
class Customer {
Integer id;
String name;
Date dob;
static void main(args) {
def customer = new Customer(id:1, name:"sunny", dob:new Date())
println "dob is $customer.dob!"
assert Byte.MAX_VALUE == 127
assert Byte.MIN_VALUE == -128
def i = 0, j = 0
Thread.start {
while(true) {
i++
if (i%1000 == 0 && i/1000 < 3 ) println 'S'
}
}
while(true) {
j++
if (j%1000 == 0 && i/1000 < 3 ) println 'M'
}
}
}
输出:
dob is Fri Jul 22 17:54:27 CST 2011!
M
S
M
S
M
三、文件操作
package groovy
class FileOperator {
public static void main(String[] args) {
def myFile = new File("C:\\ftnstat.stat")
def printFileLine = {println it}
myFile.eachLine( printFileLine )
}
}
输出:
MaxFileUploadParallelNum:0\MaxFileWaitingNum:0\SelectFileTimes:0\UploadFileNum:0\
MaxFileUploadParallelNum:0\MaxFileWaitingNum:0\SelectFileTimes:0\UploadFileNum:0\
MaxFileUploadParallelNum:0\MaxFileWaitingNum:0\SelectFileTimes:0\UploadFileNum:0\
MaxFileUploadParallelNum:0\MaxFileWaitingNum:0\SelectFileTimes:0\UploadFileNum:0\
MaxFileUploadParallelNum:0\MaxFileWaitingNum:0\SelectFileTimes:0\UploadFileNum:0\
四、数据库操作
package groovy
class SqlOperator {
public static void main(String[] args) {
def sql = groovy.sql.Sql.newInstance( "jdbc:oracle:thin:@192.168.0.201:1521:oracle9i",
"gz", "gz", "oracle.jdbc.driver.OracleDriver" )
def results = []
sql.eachRow("select * from acl_user", {
results<<it
println it.id + " -- ${it.name} "
})
println "size = "+results.size()
}
}
输出:
1227978 -- 钟润甜
1227982 -- 何毅永
792275 -- 赵清喆
1467305 -- 李沈藏
934266 -- 麦燕芬
1596070 -- 徐月平
1792352 -- 吴玉燕
788199 -- 冯晓红
1920836 -- 梁剑梅
1927141 -- 邬国威
size = 10
分享到:
相关推荐
最简单的hello world基于 Groovy 和 Ubuntu 12.04 LTS.
这个Github公共存储库包含用所有计算机语言编写的Hello World程序。 电脑语言 //文件扩展名| 电脑语言 ABAP | 高级业务应用程序编程 ADA | Ada高级编程语言 英美烟草| 批 高炉| Brainfuck神秘编程语言 C | C计算机...
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 ...
apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望...
and provides,, # Tips and Tricks, # Unit Testing and Build Support, # Groovy on Windows, An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework.
groovy入门经典,groovyeclipse 插件
赠送jar包:groovy-3.0.9.jar; 赠送原API文档:groovy-3.0.9-javadoc.jar; 赠送源代码:groovy-3.0.9-sources.jar; 赠送Maven依赖信息文件:groovy-3.0.9.pom; 包含翻译后的API文档:groovy-3.0.9-javadoc-API...
Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...
Groovy
groovy
Java调用Groovy,实时动态加载数据库groovy脚本,java读取mongoDB的groovy脚本,加载实时运行,热部署
Groovy轻松入门—搭建Groovy开发环境 Groovy轻松入门—搭建Groovy开发环境
groovy-2.3.6-installer windows安装版本
groovy的帮助文档 !!!!!
groovy-common-extensions.zip,groovy 2+的扩展集合。
groovy eclipse plugin2groovy eclipse plugin2groovy eclipse plugin2groovy eclipse plugin2groovy eclipse plugin2groovy eclipse plugin2groovy eclipse plugin2
什么是 Groovy? Groovy 是 JVM 的一个替代语言 — 替代 是指可以用 Groovy 在 Java 平台上进行 Java 编程,使用方式基本与使用 Java 代码的方式相同。在编写新应用程序时,Groovy 代码能够与 Java 代码很好地结合,...
史上最全的50多种常用的不同语言、工具Hello World示例代码 文件后缀名 描述 .applescript 苹果公司开发的一种脚本语言 .asp ASP 是微软开发的在服务器端脚本环境 .aspx ASPX 是微软的在服务器端运行的动态网页文件 ...
groovy 源码
Groovy 教程。简单的groovy教程。