Groovy基础语法
变量
引号区别
Groovy可以用单引号、双引号、三引号来表示字符串,但是各自还是有区别的。
- 单引号
内部不能引用扩展表达式,如下的 name 字段 是不能引用到的。
要处理特殊字符,比如换行 、内部引号、斜杠等
def name = 'test'
def sex = 'name : ${name}'
def age = 'arg \n 33'
- 双引号
可以引用 扩展表达式
def name = "android"
def sayhello = "hello: ${name}"
- 三引号
跟单引号一样,不能引用扩展表达式。但是可以自由空格换行。
def thupleName = '''\
line one
line two
line three
'''
string用法
- 比较
def str2 = 'hello'
def str3 = "123"
println str2.compareToIgnoreCase(str)
println str > str2
- 截取
def str = "groovy hello"
//结果 ro
println str[1..2]
- 减法
def str = "groovy hello"
def str2 = 'hello'
//输出 groovy
println str.minus(str2)
- 反转
println str.reverse()
- 判断是否有数字
println str.isNumber()
- 类型转换
//注意类型转换错误
println str3.toInteger()
闭包
很多开发语言都有这个概念,kotlin也有,这里不多解释。
声明、调用、返回
声明一个闭包,结构如下:
{参数 -> 闭包体}
->是来分割参数和闭包体内容的
def clouser = { String name , int age->
println("hello 闭包 ${name},arg==${age}")
}
clouser.call('hujin',25)
调用有2种方法:一、直接clouser(参数)
二、方法调用clouser.call(参数)
为养好习惯,避免与方法相似,请使用call这种
闭包 是有一个隐式参数it的
def defclouser = {
println("hello 闭包 ${it}")
}
defclouser.call('groovy')
如果调用不传参数,则it为null。
闭包默认是不执行返回的,但默认会返回null,可以使用return返回
def retClouser = {
return it
}
def reuslt = retClouser.call('hello')
this owner delegate 区别和使用
学过 android的同学都知道this这个东西,可以理解成类自身。groovy确多了owner和delegate。一般情况下,可能这三者是一样的。但特效情况却不一样。
- this 代表 闭包定义处的类
- owner 代表闭包定义处的类或对象(闭包 还可以在包一个闭包,内部闭包)
- delegate 代表任意对象 delegate默认和owner一致,delegate是可修改的
看完解释,说不定更懵逼了。看下例子:(都在variable包下thisstudy.groovy文件中)
例子一:
def clouser = {
println(this)
println(owner)
println(delegate)
}
输出:
variable.thisstudy@7e6ef134
variable.thisstudy@7e6ef134
variable.thisstudy@7e6ef134
clouser闭包在最thisstudy.groovy文件下,不存在内部闭包,也不是内部类的闭包。所以这三者表达的意思相同,也符合定义。
例子二:
在thisstudy.groovy文件中,再创建一个类,相当于内部类:
class Person{
String name
def classClouser = {
println(this)
println(owner)
println(delegate)
}
}
Person person = new Person();
person.classClouser.call()
输出:
variable.Person@a87f8ec
variable.Person@a87f8ec
variable.Person@a87f8ec
classClouser是在Person类中定义的,按照上面的定义,this、owner、delegate都指为Person的实例,符合定义。
例子三:
在Person中添加一个方法,方法中在定一个闭包,再包一个内部闭包:
class Person{
String name
def say(){
def classClouser = {
println("method=="+this)
println("method=="+owner)
println("method=="+delegate)
def classTwoClouser = {
println("classTwoClouser=="+this)
println("classTwoClouser=="+owner)
println("classTwoClouser=="+delegate)
}
classTwoClouser.call()
}
classClouser.call()
}
}
Person person = new Person();
person.say()
method==variable.Person@a87f8ec
method==variable.Person@a87f8ec
method==variable.Person@a87f8ec
classTwoClouser==variable.Person@a87f8ec
classTwoClouser==variable.Person$_say_closure2@4879dfad
classTwoClouser==variable.Person$_say_closure2@4879dfad
方法中外包闭包还是指的方法的类,因为此时外闭包相当于是在方法外定义的,所以还是指代的Persion实例。
而方法闭包的内部闭包就不一样了,this还是指向Persion实例,符合它的定义。但是owner不一样了,这里是闭包类中的闭包,owner要指向定义处的类或对象,所以这里要指say
方法中的外闭包对象。而delegate依旧保持与owner一致。
例子四:
闭包的委托策略,就需要用到delegate了。就可能与owner不一致了。
class Student{
String name
def pretty = {"my name is ${name}"}
String toString(){
pretty.call()
}
}
class Teacher{
String name
}
class TeacherTwo{
String name1
}
def stu = new Student(name: 'student')
def tea = new Teacher(name: 'teacher')
def tea2 = new TeacherTwo(name1: 'teacher2222')
println stu.toString()
//这里打印 my name is student 正常
//开始委托 上面所说的delegate 是可以修改的
stu.pretty.delegate = tea
//改版委托策略,默认是访问owner的变量,这里要改成delegate
stu.pretty.resolveStrategy = Closure.DELEGATE_FIRST
//这里输出 my name is student studeng的name被委托给teacher的name字段了
println stu.toString()
//特殊情况
stu.pretty.delegate = tea2
//如果没有找到DELEGATE_FIRST委托下的对应字段name 那么还是用owner来找对应字段
stu.pretty.resolveStrategy = Closure.DELEGATE_FIRST
println stu.toString()
//这里打印 my name is student 没找到委托字段,所以还是用回owner。
具体看下注释。
这里今日可以看下其他委托策略:
public static final int OWNER_FIRST = 0;
public static final int DELEGATE_FIRST = 1;
public static final int OWNER_ONLY = 2;
public static final int DELEGATE_ONLY = 3;
public static final int TO_SELF = 4;
大概看名字就知道意思,大概了解下就行。
下期讲闭包与数据结构的用法