美文网首页
闭包委托策略

闭包委托策略

作者: 龙剑灵 | 来源:发表于2020-03-17 22:04 被阅读0次
    //闭包委托策略
    class Student {
        String name
        def pretty = { "My name is ${name}" }
    
        String toString() {
            pretty.call()
        }
    }
    
    class Teacher {
        String name1
    }
    
    def stu = new Student(name: "jimmy")
    def tea = new Teacher(name: "qq")
    stu.pretty.delegate = tea
    stu.pretty.resolveStrategy = Closure.DELEGATE_FIRST  //My name is qq
    println  stu.toString()
    

    相关文章

      网友评论

          本文标题:闭包委托策略

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