groovy学习3-语法学习
贴段练习代码,基本上包含了groovy强大的一些特性和语法
代码
package basic
/**
* @author <a href="mailto:czy88840616@gmail.com">czy</a>
* @since 2010-3-27 15:13:48
*
*/
println "helloworld"
println "-----"
//loop
for(i in 0..2)
println i
println "-----"
//upto
0.upto(2) {print "$it "}
println "\n-----"
//times
3.times {print "$it "}
println "\n-----"
//step
0.step (10, 2) {print "$it "}
println "\n-----"
//系统运行
println "ping".execute().text
println "-----"
println "ping".execute().getClass().name
println "-----"
println "cmd /C dir".execute().text
println "-----"
//?.操作符
def foo(str){
str?.reverse()
}
println foo('evil')
println foo(null)
println "-----"
//exception
def openFile(fileName) {
new FileInputStream(fileName)
}
try {
openFile "xxxx.txt"
}catch (e){
println "file not found:" + e
}
println "-----"
//字符串条件判断-null
str="hello"
if(str) println str
println "-----"
//集合类型默认条件判断 null or empty
lst0 = null
println lst0?'lst0 true':'lst0 false'
lst1 = [1,2,3]
println lst1 ? 'lst1 true':'lst1 false'
lst2 = []
println lst2 ? 'lst2 true':'lst2 false'
println "-----"
//<<操作符
lst=['hello']
lst << 'there'
println lst
println "-----"
//运算符重载
class ComplexNumber {
def real, imaginary
def plus(other) {
new ComplexNumber(real: real + other.real, imaginary: imaginary + other.imaginary)
}
String toString() { "$real ${imaginary > 0 ? '+' : ''} ${imaginary}i" }
}
c1 = new ComplexNumber(real: 1, imaginary: 2)
c2 = new ComplexNumber(real: 4, imaginary: 1)
println c1 + c2
println "------"
//return
def isPalindrome(str) { str == str.reverse() }
println "mom is palindrome? ${isPalindrome('mom')}"
println "-----"
//== and is()
str1 = 'hello'
str2 = str1
str3 = new String('hello' )
str4 = 'Hello'
println "str1 == str2: ${str1 == str2}"
println "str1 == str3: ${str1 == str3}"
println "str1 == str4: ${str1 == str4}"
println "str1.is(str2): ${str1.is(str2)}"
println "str1.is(str3): ${str1.is(str3)}"
println "str1.is(str4): ${str1.is(str4)}"
println "-----"
//不可以省略;
class Semi {
def val = 3;
{
println "Instance Initializer called..."
}
}
println new Semi()
println "-----"
//array
int[] arr = [1, 2, 3, 4, 5]
println arr
println "class is " + arr.getClass().name
println "-----"
//dynmic
def takeHelp(helper) {
//...
helper.helpMoveThings()
//...
}
class Man {
void helpMoveThings() {
//...
println "Man's helping"
}
//...
}
class Woman {
void helpMoveThings() {
//...
println "Woman's helping"
}
//...
}
class Elephant {
void helpMoveThings() {
//...
println "Elephant's helping"
}
void eatSugarcane() {
//...
println "I love sugarcanes..."
}
//...
}
takeHelp(new Man())
takeHelp(new Woman())
takeHelp(new Elephant())
println "-----"
//clouse
def pickEven(n, block) {
for (int i = 2; i <= n; i += 2) {
block(i)
}
}
pickEven(10) { evenNumber -> println evenNumber }
pickEven(10, { println it })
total = 0
pickEven(10) { total += it }
println "Sum of even numbers from 1 to 10 is ${total}"
product = 1
pickEven(10) { product *= it }
println "Product of even numbers from 1 to 10 is ${product}"
//闭包就是一个拥有参数的函数被绑定到上下文环境中来运行。