美文网首页
groovy(10)-闭包委托策略

groovy(10)-闭包委托策略

作者: 高斯巴 | 来源:发表于2018-11-12 15:11 被阅读0次

package variable

/*

闭包的三个重要变量:this,owner,delegate

区别在于:this代表闭包定义处最近的对象(不包含闭包),owner代表闭包定义出最近的对象可以是闭包.delegate默认与owner一致.

delegate可以修改

*/

scriptClouser={

    println("scriptClouser this::"+this)

    println("scriptClouser owner:::"+owner)

    println("scriptClouser delegate::::"+delegate)

    //所以大部分情况下 ,this,owner,delegate 是一样//以上三个输出的结果是一样

/*scriptClouser this::variable.Clousershujujiegou@17046283

scriptClouser owner:::variable.Clousershujujiegou@17046283

scriptClouser delegate::::variable.Clousershujujiegou@17046283*/

}

scriptClouser.call()

//文件中定义一个内部类

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()

//输出的都是 类的 字节码文件对象.class variable.Preson.因为是静态的方法

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()

//总结1:对象中包含闭包:this,owner,delegate指向的都是离他最近的一个类或对象

//scriptClouser中的this,owner,delegate ,输出结果是variable.Clousershujujiegou@17046283(即文件对应的对象)

//对应的静态方法中Person中输出的是class variable.Preson ,Person2中输出的是variable.Preson2@1dde4cb2(即文件中包含闭包的类对应的对象)

//闭包中定义一个闭包

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()

//总结2:闭包中包含闭包:this指向他所定义处的文件对象,owner指向离他最近的闭包nestCloust的一个对象,二delegate默认和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是通过构造方法传递的,我们通过更改其委托,让他指向teacher对象的name

stu.pretty.delegate=teacher

//将stu中的pretty闭包的delegate指向为teacher对象,但是因为其默认的委托策略还是Closure.OWNER_FIRST(优先从owner中找),所以要设置为Closure.DELEGATE_FIRST(让他优先从delegate中找)

stu.pretty.resolveStrategy=Closure.DELEGATE_FIRST

println(stu.toString())//my name is lilei

//当我们把Teacher中name去掉后,delegate中找不到时,他又会从owner中去找,所以讲teacher中的name去掉后,结果是:my name is lich.当把stu的委托策略改成Closure.DELEGATE_ONLY时,会报错,因为此时找不到name

相关文章

  • groovy(10)-闭包委托策略

    package variable /* 闭包的三个重要变量:this,owner,delegate 区别在于:th...

  • 闭包委托策略

  • 详解 groovy 的闭包(上)

    groovy 的闭包特点 在 groovy 中的闭包。groovy 中的闭包是一个开放的匿名代码块,可以接受参数,...

  • Groovy 闭包

    本文介绍了Groovy闭包的有关内容。闭包可以说是Groovy中最重要的功能了。如果没有闭包,那么Groovy除了...

  • 深入理解闭包中的委托

    关于闭包的使用在官方文档中已经说明的很清楚了,这里再对闭包的委托机制进行说明,该机制对于 Groovy 开发,gr...

  • Groovy语法简介

    Groovy简单语法: Groovy中的闭包:

  • 二、Groovy语法(二):闭包

    Groovy闭包 1、Groovy中闭包基础 1.1 闭包的概念闭包是被包装成对象的代码块,可以通过一个变量引用到...

  • 《Groovy极简教程》第9章 Groovy闭包(Closure

    《Groovy极简教程》第9章 Groovy闭包(Closures)

  • Groovy 闭包

    好久没动笔(键盘)了,发现自己变懒了。但是在实际的工作过程中,总是责怪自己没有动笔把自己之前的遇到的问题记录下来,...

  • Groovy 闭包

    闭包 闭包是一段可执行的代码块,类似于方法也可以传递参数;可在闭包中访问属性值,也就是说可以修改闭包作用域中的所有...

网友评论

      本文标题:groovy(10)-闭包委托策略

      本文链接:https://www.haomeiwen.com/subject/abfcfqtx.html