语法基础——Groovy语法基础变量
1、变量类型
groovy变量没有基本数据类型,只有引⽤类型,尽管定义的基本类型也会被转换成引⽤类型
int x = 10
println x.class//class java.lang.Integer
double y = 10.10
println y.class//class java.lang.Double
2、弱类型
groovy变量可以有强类型⽅式和弱类型⽅式,弱类型⽅式会⾃动转换成对应的引⽤类型
def z = 'name'
println z.class //class java.lang.String
def h = 1.34
println h.class //class java.math.BigDecimal
字符串
1、⽆格式定义的字符串
⽆格式定义的字符串指的是输出的时候,字符串不会带有原本输⼊的格式
def name = 'Hensen'
2、有格式定义的字符串
有格式定义的字符串指的是输出的时候,会按照原本定义的格式进⾏输出
def name = '''\
line one
line two
line three
'''
3、GString
groovy提供新的字符串类型GString,⽤双引号定义的字符串表⽰可拓展的字符串
def name = "Hensen"
def say = "${name} say : Hello groovy"
println say //Hensen say : Hello groovy
println say.class //vy.runtime.GStringImpl
def sum = "${2 + 3}"
println sum //5
4、字符串Api
groovy的字符串提供了很多Api对字符串进⾏操作
def str = "groovy"
(10,'1') //11groovy11
println str.padLeft(10,'1') //1111groovy
println str.padRight(10,'1') //groovy1111
def str2 = "java"
println str > str2 //false
println str[0] //g
println str[0..2] //grp
def str3 = "a"
println str2 - str3 //jva
verse() //yvoorg
println str.capitalize() //Groovy
println str.isNumber() //false
def str4 = "2"
Long() //2
逻辑控制
1、switch-case
groovy提供的switch语句可以判断类型的参数
def x = 5.2
def result
switch (x){
case'you':
result = "you"
break
case [3,4,5,"list"]:
result = "inList"
break
case12..30:
result = "12 to 30"
break
case Integer:
result = "Integer Params"
break
switch case判断字符串case BigDecimal:
result = "BigDecimal Params"
break
default:result="default result"
}
println result //BigDecimal Params
2、for
//遍历范围
def sum = 0
for(i in 0..9){
sum += i
}
println sum //45
//遍历集合
def sum2 = 0
for (i in [0,1,2,3,4,5]){
sum2 += i
}
println sum2 //15
//遍历Map
def sum3 = 0
for(i in ["one":1,"two":2,"three":3]){
sum3 += i.value
}
println sum3 //6
闭包
1、闭包
//⽆参闭包
def method = {println "Hello groovy"}
//有参闭包
def method2 = {String name -> println "Hello ${name}"}
//默认参数闭包
def method3 = {println "Hello ${it}"}
//带返回值闭包
def method4 = {return"Hello ${it}"}
//闭包的调⽤
def name = "groovy"
method.call()
method2(name)
2、闭包函数
def result = fab(5)
def result2 = fab2(5)
def result3 = cal(5)
println result //120
println result2 //120
println result3 //11
int fab (int number){
int result = 1
1.upto(number,{ num -> result *= num }) //执⾏1-number的闭包
return result
}
int fab2 (int number){
int result = 1
number.downto(1,{ num -> result *= num }) //执⾏number-1的闭包
return result
}
int cal (int number){
int result = 1
number.times { num -> result += num} //执⾏0-number的闭包
return result
}
3、字符串闭包函数
def intro = "my name is Hensen,my age is 18"
//到第⼀个符合条件的字符
println intro.find {
String s -> s.isNumber() //1
}
//到所有符合条件的字符
println intro.findAll {
String s -> s.isNumber() //[1, 8]
}
//有⼀项字符符合即可
println intro.any {
String s -> s.isNumber() //true
}
//所有字符必须符合条件
println intro.every {
String s -> s.isNumber() //false
}
//将字符串转换成集合
llect {
//遍历所有字符
intro.each {
UpperCase() //MY NAME IS HENSEN,MY AGE IS 18
}
4、闭包关键字
闭包的关键字分为下⾯三个
this:代表当前闭包定义处的类
owner:代表当前闭包定义处的类或者对象
delegate:代表任意对象,默认与owner⼀致
⼀、正常闭包
在这⾥的this、owner、delegate表⽰同⼀个对象,即outer
def outer = {
println "outer this:" + this
println "outer owner:" + owner
println "outer delegate:" + delegate
}
outer.call()
//输出结果
outer this:Chapter4o4@f48007e
outer owner:Chapter4o4@f48007e
outer delegate:Chapter4o4@f48007e
⼆、嵌套闭包
在这⾥的this表⽰outer2、这⾥的owner、delegate表⽰inner
def outer2 = {
def inner = {
println "inner this:" + this
println "inner owner:" + owner
println "inner delegate:" + delegate
}
inner.call()
}
outer2.call()
//输出结果
inner this:Chapter4o4@f48007e
inner owner:Chapter4o4$_run_closure2@11cfefda
inner delegate:Chapter4o4$_run_closure2@11cfefda
三、委托策略
delegate关键字跟委托策略有关,委托策略有四种
DELEGATE_FIRST:先从Delegate去委托属性,再从Owner去委托属性DELEGATE_ONLY:只从Delegate去委托属性
OWNER_FIRST:先从Owner去委托属性,再先从Delegate去委托属性OWNER_ONLY:只从Owner去委托属性
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论