字符串:
def str1 = 'hello'
def str2 = "$str1 world" //双引号中可以有表达式运算,输出hello world
def str3 = /\d\r\n\a\b/ //str3 =='\\d\\r\\n\\a\\b'
def str4 = """
hello
world
hello
grails
""" //输出"\nhello\nworld\nhello\ngrails\n"
str1*2 == "hellohello"
str2-str1 == ' world'
str2[0..3]=='ell'
str2[-3..-1] == 'rld'
str1.toList() ==["h","e","l","l","o"]
str1.reverse() == "olleh"
str2.size == 20
数字:
def x = 3
def y =4
x+y == 7
x.plus(y) == 7
x instanceof Integer
def a = 2/3 //结果0.6666666667
a instanceof BigDecimal
a = 2.intdiv(3)
a instanceof Integer
def b = a.setScale(3,BigDecimal.ROUND_HALF_UP)
b == 0.667
类:
class A{
String p1
String p2
}
def a = new A(p1:"XX",p2:"yy")
a.p1 == "XX"
a.p2 == "yy"
class A {
String p1
}
class A {
private String p1
public String getP1(){p1}
public void setP1(String p1)( this.p1 = p1)
}
以上两个类完全等价。
并且groovy中,a.p1 == a.getP1()
a.p1 == "YY" a.setP1("YY")
a+b 实际上是调用了a的plus方法。
如果要求一个类可以相加,在类中添加此方法即可。
List
def list1 = [1,2,3]
list1 *2 == [1,2,3,1,2,3]
list1.unique() ==[1,2,3]
list1 = [1,2,3,1,2,3]
list1.unique() == (list1 as Set) as List
list1.size() == 6
list1.reverse() == [3,2,1,3,2,1]
[1,2,3,4] + [5] == [1,2,3,4,5]
[1,2,3,4] << [5] == [1,2,3,4,5]
[1,2,3,4] + 5 == [1,2,3,4,5]
[1,2,3,4,1] - [1] == [2,3,4]
[1,2,3,4,1] - 1 == [2,3,4]
[1,2,3,[4,5]].flatten() == [1,2,3,4,5] //展开List中的List
[1,2,3,4].max() == 4
[1,2,3,4].min() == 1
[1,2,3,4,1].count(1) == 2
[1,2,3,4].sum() == 10
[1,3,2,4].sort() == [1,2,3,4]
[1,2,3,4].disjoint([...]) //一个List与另一个List是否存在交集,return boolean
[1,2,3,4].intersect([...]) //求一个List与另一个List的交集
List*.unique() //对List中的每个元素都执行后面的方法
(1..9).each({ }) //遍历里面的所有元素
findAll({ it>4 }) //返回真的元素组成一个新的List
collect({ it*it }) //返回值构造成新的列表
any({ it*it == it+6 }) //用于判断集合是否有符合条件的元素。至少存在一个元素符合条件时,返回真,否则假
every({ it*it == it+6 }) //用于判断集合是否有符合条件的元素。当集合所有元素都符合条件时,返回真,否则假
Map:
def map =[key1:"vlaue1"]
map == ["key1":"value1"]
map.key1 == "value1"
map["key1"] == "value1"
map.keySet() == ["key1"]
def key = "key123"
def map1 = [key:"value"]
map1.key == 'value'
map1[key] == null
def map2 = [(key):"value"]
map2 == ["key123":"value"]
map1["newKey"] = "newValue"
map1 == ["key":"value","newKey":"newValue"]
map1.remove("key")
map1.key == null
[key1:"value1",key2:"value2"].each({ entry -> println "$entry.key $entry.value" }) //对Map的遍历
区间:
def range1 == 1..5
def range2 == 1..<5
range1.contains(5)
!range2.contains(5)
range1.size() == 5
range2.size() == 4
def today == new Date()
def yesterday = today - 1
(yesterday..today).size() == 2
('a'..'c').contains('b')
'b' in ('a'..'c')
//区间用于循环
def log = ' '
for (i in 1..5){
log+=i
}
log == '12345'
//区间用于switch
def score = 36
switch(score){
case 0..60:
println '不及格'
break //注:break要换行写
case 60..<70:
println '及格'
break
case 70..<85:
println '良好'
break
case 85..<100:
println '优秀'
break
case 100:
println '完美'
break
default:throw new IllegalArgumentException()
}
输出:不及格
[*range1] == [1,2,3,4,5]//前置*可将区间展开,用于构造List
闭包:
def c1 = {println it}//当没有->时,可用it的变量访问参数
def c2 = {text ->println text}
c1.call("XX") //用call方法调用闭包
c2("content2") //直接调用闭包
动态添加方法:
1、类名.metaClass.方法名 = 闭包
2、类名.metaClass.方法名 << 闭包
如果新加入的方法名与参数都和旧的完全一样,使用"="会覆盖旧的方法,使用“<<”会报错。
如果添加的方法带参数,闭包中要添加“输入参数”。
Foo.metaClass.speak = { def num -> //无指定类型的参数
}
Foo.metaClass.speak = { int num -> //强制类型的参数,如果传入的参数类型不对,会报错
}
Foo.metaClass.speak = { -> //强制无参数,如果传入参数,会报错
}
补充domain中的方法:beforeUpdate/Insert/Delete