package variable
/*
閉包的三個重要變量:this,owner,delegate
區(qū)別在于:this代表閉包定義處最近的對象(不包含閉包),owner代表閉包定義出最近的對象可以是閉包.delegate默認(rèn)與owner一致.
delegate可以修改
*/
scriptClouser={
????println("scriptClouser this::"+this)
????println("scriptClouser owner:::"+owner)
????println("scriptClouser delegate::::"+delegate)
????//所以大部分情況下 ,this,owner,delegate 是一樣//以上三個輸出的結(jié)果是一樣
/*scriptClouser this::variable.Clousershujujiegou@17046283
scriptClouser owner:::variable.Clousershujujiegou@17046283
scriptClouser delegate::::variable.Clousershujujiegou@17046283*/
}
scriptClouser.call()
//文件中定義一個內(nèi)部類
class Preson{
//類中的閉包
????def static classClouse={
????println("bscriptClouser this::"+this)
????//bscriptClouser this::classvariable.Preson
????println("bscriptClouser owner:::"+owner)
????//bscriptClouser owner:::class ????variable.Preson
????println("bscriptClouser delegate::::"+delegate)
????//bscriptClouser ????delegate::::class variable.Preson
}
//類中的方法的閉包
????def static fun(){? ??
????????????def? classClouse={? ? ? ??
????????????????????println("funscriptClouser this::"+this)
????????????????????//funscriptClouser this::class variable.Preson
????????????????????println("funscriptClouser owner:::"+owner)
????????????????????//funscriptClouser owner:::class variable.Preson
? ? ? ? ? ? ? ? ? ? ?println("funscriptClouser delegate::::"+delegate)
????????????????????//funscriptClouser delegate::::class variable.Preson
? ? ? ? ? ? ? ?}? ??
? ? ? ? ? ? ? classClouse.call()}
}
Preson.classClouse.call()
Preson.fun()
//輸出的都是 類的 字節(jié)碼文件對象.class variable.Preson.因?yàn)槭庆o態(tài)的方法
class Preson2{
//類中的閉包
????def? classClouse={? ??
????????????println("p2bscriptClouser this::"+this)
????????????//p2bscriptClouser this::variable.Preson2@1dde4cb2
????????????println("p2bscriptClouser owner:::"+owner)
????????????//p2bscriptClouser owner:::variable.Preson2@1dde4cb2
????????????println("p2bscriptClouser delegate::::"+delegate)
? ? ? ? ? ? //p2bscriptClouser delegate::::variable.Preson2@1dde4cb2
????}
????????????//類中的方法的閉包
? ?????def fun(){? ??
? ? ? ? ? ? ?def? classClouse={? ? ? ??
????????????????????????println("p2funscriptClouser this::"+this)
? ? ? ? ? ? ? ? ? ? ? ? //p2funscriptClouser this::variable.Preson2@1dde4cb2
? ? ? ? ? ? ? ? ? ? ? ? println("p2funscriptClouser owner:::"+owner)
????????????????????????//p2funscriptClouser owner:::variable.Preson2@1dde4cb2
????????????????????????println("p2funscriptClouser delegate::::"+delegate)
????????????????????????//p2funscriptClouser delegate::::variable.Preson2@1dde4cb2
? ? ? ? ? ? ? ?}? ??
????????????classClouse.call()}
????????}
}
Preson2 p2=new Preson2();
p2.classClouse.call()
p2.fun()
//總結(jié)1:對象中包含閉包:this,owner,delegate指向的都是離他最近的一個類或?qū)ο?/b>
//scriptClouser中的this,owner,delegate ,輸出結(jié)果是variable.Clousershujujiegou@17046283(即文件對應(yīng)的對象)
//對應(yīng)的靜態(tài)方法中Person中輸出的是class variable.Preson ,Person2中輸出的是variable.Preson2@1dde4cb2(即文件中包含閉包的類對應(yīng)的對象)
//閉包中定義一個閉包
def nestClouser={
????????def innerClouser={? ??
????????????????println("innerfunscriptClouser this::"+this)
? ? ? ? ? ? ? ? //innerfunscriptClouser this::variable.Clousershujujiegou@9225652
????????????????println("innerfunscriptClouser owner:::"+owner)
? ? ?//innerfunscriptClouser owner:::variable.Clousershujujiegou$_run_closure2@72057ecf
????????????????println("innerfunscriptClouser delegate::::"+delegate)
? ? //innerfunscriptClouser? delegate::::variable.Clousershujujiegou$_run_closure2@72057ecf
????????????}
????????innerClouser.delegate=p2
????????//delegate修改后:innerfunscriptClouser ????delegate::::variable.Preson2@6eda5c9
????????innerClouser.call()
}
nestClouser.call()
//總結(jié)2:閉包中包含閉包:this指向他所定義處的文件對象,owner指向離他最近的閉包nestCloust的一個對象,二delegate默認(rèn)和owner一致.
/*
閉包的委托策略
Closure.OWNER_FIRST
Closure.OWNER_ONLY
Closure.DELEGATE_FIRST
Closure.DELEGATE_ONLY
*/
class Student{
????String name
? ? ?def pretty={"my name is ${name}"}
? ? ?StringtoString(){? ? pretty.call()}
}
class Teacher{
????String name
}
def stu=new Student(name:'lich')
def teacher=new Teacher(name:'lilei')
println(stu.toString())//my name is lich.
//stu的name是通過構(gòu)造方法傳遞的,我們通過更改其委托,讓他指向teacher對象的name
stu.pretty.delegate=teacher
//將stu中的pretty閉包的delegate指向?yàn)閠eacher對象,但是因?yàn)槠淠J(rèn)的委托策略還是Closure.OWNER_FIRST(優(yōu)先從owner中找),所以要設(shè)置為Closure.DELEGATE_FIRST(讓他優(yōu)先從delegate中找)
stu.pretty.resolveStrategy=Closure.DELEGATE_FIRST
println(stu.toString())//my name is lilei
//當(dāng)我們把Teacher中name去掉后,delegate中找不到時,他又會從owner中去找,所以講teacher中的name去掉后,結(jié)果是:my name is lich.當(dāng)把stu的委托策略改成Closure.DELEGATE_ONLY時,會報錯,因?yàn)榇藭r找不到name