`
cutesunshineriver
  • 浏览: 194915 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Hello World on Groovy

阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics