【Groovy】闭包 Closure ( 闭包的 delegate 代理策略 | OWNER_FIRST | DELEGATE_FIRST | OWNER_ONLY | DELEGATE_ONLY )

简介: 【Groovy】闭包 Closure ( 闭包的 delegate 代理策略 | OWNER_FIRST | DELEGATE_FIRST | OWNER_ONLY | DELEGATE_ONLY )

文章目录

一、闭包的代理策略引入

二、闭包的四种代理策略





一、闭包的代理策略引入


分别在 Groovy 脚本 和 Test 类中定义 fun 方法 , 打印不同的内容 ;


在闭包 Closure 中分别调用上述两个方法 ,



代码示例 :


// I. 闭包中执行 Groovy 脚本中的方法
def fun() {
    println "fun"
}
def closure = {
    fun()
}
closure()
// II. 闭包中执行 Test 类中的方法
class Test {
    def fun() {
        println "fun2"
    }
}
// 闭包中不能直接调用 Test 对象中的方法
// 此时可以通过改变闭包代理进行调用
def closure2 = {
    fun()
}
// 设置闭包的代理
closure2.delegate = new Test()
// 执行闭包
closure2()



上述代码执行结果 :

image.png



执行上述代码后 , 两个闭包都执行的是 Groovy 脚本中的 fun 方法 ;


即使 closure2 闭包设置的是 Test 实例对象作为代理 , 其仍然执行的是 Groovy 脚本中的 fun 方法 ;


// 设置闭包的代理
closure2.delegate = new Test()


这是因为 Closure 闭包的 代理策略问题导致的 ;


在 Closure 闭包中 , resolveStrategy 成员配置的是该闭包的代理策略 , 默认的代理策略 OWNER_FIRST , 也就是优先从 owner 中查找方法 ; 此时即使在 delegate 中有 fun 方法 , 也会被 owner 中的 fun 方法覆盖 ;


private int resolveStrategy = OWNER_FIRST;






二、闭包的四种代理策略


闭包的四种代理策略 :


OWNER_FIRST : 所有者中的方法优先 ;

DELEGATE_FIRST : 代理优先策略 , 代理中的方法优先 ;

OWNER_ONLY : 只执行所有者中的方法 ;

DELEGATE_ONLY : 只执行代理中的方法 ;

TO_SELF : 只在自身查找 ;

public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {


 

/**
     * 设置此resolveStrategy后,闭包将首先尝试将属性引用和方法解析给所有者,
     * 然后是委托(<b>这是默认策略)。
     *
     * For example the following code :
     * <pre>
     * class Test {
     *     def x = 30
     *     def y = 40
     *
     *     def run() {
     *         def data = [ x: 10, y: 20 ]
     *         def cl = { y = x + y }
     *         cl.delegate = data
     *         cl()
     *         println x
     *         println y
     *         println data
     *     }
     * }
     *
     * new Test().run()
     * </pre>
     * will output :
     * <pre>
     * 30
     * 70
     * [x:10, y:20]
     * </pre>
     * 因为在测试类中声明的x和y字段在委托中定义了变量<p> 
     * <i>请注意,总是首先查找局部变量,而与解析策略无关。</i>
     */
    public static final int OWNER_FIRST = 0;
    /**
     * 设置此resolveStrategy后,闭包将首先尝试将属性引用和方法解析为委托,然后解析为所有者。
     *
     * 例如,以下代码:
     * <pre>
     * class Test {
     *     def x = 30
     *     def y = 40
     *
     *     def run() {
     *         def data = [ x: 10, y: 20 ]
     *         def cl = { y = x + y }
     *         cl.delegate = data
     *         cl.resolveStrategy = Closure.DELEGATE_FIRST
     *         cl()
     *         println x
     *         println y
     *         println data
     *     }
     * }
     *
     * new Test().run()
     * </pre>
     * will output :
     * <pre>
     * 30
     * 40
     * [x:10, y:30]
     * </pre>
     * 因为在测试类中声明的x和y字段在委托中定义了变量<p> 
     * <i>请注意,总是首先查找局部变量,而与解析策略无关。</i>
     */
    public static final int DELEGATE_FIRST = 1;
    /**
     * 使用此resolveStrategy集,闭包将只解析所有者的属性引用和方法,而不调用委托。
     * 例如,以下代码:
     *
     * <pre>
     * class Test {
     *     def x = 30
     *     def y = 40
     *
     *     def run() {
     *         def data = [ x: 10, y: 20, z: 30 ]
     *         def cl = { y = x + y + z }
     *         cl.delegate = data
     *         cl.resolveStrategy = Closure.OWNER_ONLY
     *         cl()
     *         println x
     *         println y
     *         println data
     *     }
     * }
     *
     * new Test().run()
     * </pre>
     *
     * 将抛出“No-this-property:z”错误,因为即使在委托中声明了z变量,也不会进行查找<p> 
     * <i>请注意,总是首先查找局部变量,而与解析策略无关</我>
     */
    public static final int OWNER_ONLY = 2;
    /**
     * 使用此resolveStrategy集,闭包将只解析委托的属性引用和方法,并完全绕过所有者。
     * 例如,以下代码:
     *
     * <pre>
     * class Test {
     *     def x = 30
     *     def y = 40
     *     def z = 50
     *
     *     def run() {
     *         def data = [ x: 10, y: 20 ]
     *         def cl = { y = x + y + z }
     *         cl.delegate = data
     *         cl.resolveStrategy = Closure.DELEGATE_ONLY
     *         cl()
     *         println x
     *         println y
     *         println data
     *     }
     * }
     *
     * new Test().run()
     * </pre>
     *
     * 将引发错误,因为即使所有者声明了“z”字段,解析策略也将绕过所有者中的查找<p> 
     * <i>请注意,总是首先查找局部变量,而与解析策略无关</i>
     */
    public static final int DELEGATE_ONLY = 3;
    /**
     * 使用此resolveStrategy集,闭包将解析自身的属性引用,
     * 并执行通常的元类查找过程。
     * 这意味着属性和方法既不能从所有者也不能从委托解析,
     * 只能在闭包对象本身上解析。
     * 这允许开发人员使用闭包本身的ExpandoMetaClass覆盖getProperty。<p>
     * <i>请注意,总是首先查找局部变量,与解析策略无关。</i>
     */
    public static final int TO_SELF = 4;
  private int resolveStrategy = OWNER_FIRST;
}


目录
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8083 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10368 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1567 5
|
9天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2715 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2483 0