文章目录

  • 一、报错信息
  • 二、解决方案

一、报错信息


在 Groovy 中的 Closure 闭包中 , 直接调用外部对象的方法 , 会报错 ;

class Test {def fun() {println "fun"}
}def closure = {fun()
}closure()

报错信息 :

"D:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-Dtools.jar=D:\Program Files\Java\jdk1.8.0_221\lib\tools.jar" -Dgroovy.home=C:\Users\octop\.gradle\caches\modules-2\files-2.1\org.codehaus.groovy\groovy-all\2.3.11\f6b34997d04c1538ce451d3955298f46fdb4dbd4 "-javaagent:Y:\001_DevelopTools\006_IntelliJ_IDEA_Community\IntelliJ IDEA Community Edition 2019.3.1\lib\idea_rt.jar=14846:Y:\001_DevelopTools\006_IntelliJ_IDEA_Community\IntelliJ IDEA Community Edition 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\octop\.gradle\caches\modules-2\files-2.1\org.codehaus.groovy\groovy-all\2.3.11\f6b34997d04c1538ce451d3955298f46fdb4dbd4\groovy-all-2.3.11.jar org.codehaus.groovy.tools.GroovyStarter --main groovy.ui.GroovyMain --classpath .;Y:\002_WorkSpace\003_IDEA\Groovy_Demo\build\classes\groovy\main;C:\Users\octop\.gradle\caches\modules-2\files-2.1\org.codehaus.groovy\groovy-all\2.3.11\f6b34997d04c1538ce451d3955298f46fdb4dbd4\groovy-all-2.3.11.jar --encoding=UTF-8 Y:\002_WorkSpace\003_IDEA\Groovy_Demo\src\main\groovy\Groovy.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: Groovy.fun() is applicable for argument types: () values: []
Possible solutions: run(), run(), run(java.io.File, [Ljava.lang.String;), find(), find(groovy.lang.Closure), any()
groovy.lang.MissingMethodException: No signature of method: Groovy.fun() is applicable for argument types: () values: []
Possible solutions: run(), run(), run(java.io.File, [Ljava.lang.String;), find(), find(groovy.lang.Closure), any()at Groovy$_run_closure1.doCall(Groovy.groovy:10)at Groovy$_run_closure1.doCall(Groovy.groovy)at Groovy.run(Groovy.groovy:14)Process finished with exit code 1

二、解决方案


在 Closure 闭包中 , 如果要调用外部对象的方法 , 需要先设置 Closure 闭包对象的 delegate 成员为指定的外部对象 ;

class Test {def fun() {println "fun"}
}// 闭包中不能直接调用 Test 对象中的方法
// 此时可以通过改变闭包代理进行调用
def closure = {fun()
}closure.delegate = new Test()
closure()

设置完 Closure 闭包对象的 delegate 之后 , 的执行效果 :

【错误记录】Groovy 闭包使用报错 ( 闭包中不能直接使用外部对象的方法 | 需要先设置 delegate 代理 )相关推荐

  1. 【错误记录】Git 使用报错 ( git branch -a 仍能查询到已经删除的远程分支 )

    文章目录 一.报错信息 二.解决方案 一.报错信息 之前已经执行 git push origin --delete feature1 命令 , 删除了 feature1 远程分支 , 删除操作成功 , ...

  2. 【错误记录】Git 使用报错 ( git: ‘switch‘ is not a git command. See ‘git --help‘. )

    文章目录 一.报错信息 二.解决方案 一.报错信息 执行 git switch -c feature1 命令 , 创建分支 , 报如下错误 : D:\Git\git-learning-course&g ...

  3. 【错误记录】Git 使用报错 ( error: The branch ‘feature1‘ is not fully merged. )

    文章目录 一.报错信息 二.解决方案 一.报错信息 执行 git branch -d feature1 命令 , 删除 feature1 分支 , 报如下错误 : D:\Git\git-learnin ...

  4. 【错误记录】Git 使用报错 ( error: Cannot delete branch ‘dev‘ checked out at ‘D:/Git/git-learning-course‘)

    文章目录 一.报错信息 二.解决方案 一.报错信息 使用 Git 操作 版本库 , 删除分支时 , 报如下错误 : D:\Git\git-learning-course>git branch - ...

  5. 【错误记录】Git 使用报错 ( no changes added to commit (use “git add“ and/or “git commit -a“) )

    文章目录 一.报错信息 二.解决方案 一.报错信息 修改了 Git 版本库中的 file1.txt 文件 , 直接执行 git commit -m "modify file1" 命 ...

  6. 【错误记录】VMware 虚拟机报错 ( VMWare 中的 Ubuntu 虚拟机网络设置 | 第一次网络设置 )

    文章目录 一.报错信息 二.解决方案 一.报错信息 在 VMWare Workstation 中创建 Ubuntu 虚拟机后 , 无法连接网络 ; 二.解决方案 在 Windows 开始菜单中 , 选 ...

  7. matlab中dmodce函数使用报错,matlab中randi函数

    ④矩形脉冲信号 矩形脉冲信号在 MATLAB 中用 rectpuls 函数表示,其调用形式为 ft...(1,n); >>xn3=randi(n,1,n); >>subplot ...

  8. sigar 使用报错问题解决(java.lang.UnsatisfiedLinkError: gather)

    sigar 使用报错问题解决(java.lang.UnsatisfiedLinkError: gather) 解决方法 1. 将sigar.jar  sigar-amd64-winnt.dll sig ...

  9. 记录webpack使用问题,使用报错“UnhandledPromiseRejectionWarning,file-loader图片过大,无法加载图片,打包html文件报错TypeError

    记录webpack使用报错 版本号问题 运行npm run build,报错 "UnhandledPromiseRejectionWarning: TypeError: this.getRe ...

最新文章

  1. MySQL-数据库和表的基本操作
  2. SQLSERVER中如何忽略索引提示
  3. 机器学习--近邻成分分析(NCA)算法 和 度量学习
  4. 【LeetCode - 1047】删除字符串中的所有相邻重复项(栈)
  5. 0706 - 个人品牌建立
  6. 【专题三】如何考量虚拟化的投资回报率?——服务器虚拟化的阴暗面
  7. Delphi的MessageBox对话框使用
  8. 加权二叉树的实现与单元测试(python)
  9. Learning Music Notation 学习音乐符号 Lynda课程中文字幕
  10. 中望3D 2019破解补丁|中望3D 2019注册破解补丁下载(附许可破解文件及破解教程)
  11. 【Android】蓝牙开发——经典蓝牙:配对与解除配对 实现配对或连接时不弹出配对框
  12. 单词串联记忆 - 故事 - Party上浪漫的事后传
  13. make[2]: *** [/home/nnnn/calibration/devel/lib/libcalibrationtoolkit.so] Error 1
  14. 泰兰德的记忆·悲情伊利丹
  15. Python爬取动态数据
  16. 西门子1200 总线控制V90伺服程序模板 两种控制模式 1.基于111报文自己编写的PN通讯控制V90伺服程序
  17. 用java语言编写程序计算九宫图
  18. 聚焦云上安全,2021首届-西部云安全峰会将在西安召开
  19. 接口鉴权 - 学习/实践
  20. GMS 发短信的515错误

热门文章

  1. 那些年借“云”出海的日子
  2. Java虚拟机学习 - 类加载器(ClassLoader)
  3. git的基本使用-1
  4. AlertDialog中的EditText不能输入
  5. 20个必不可少的Python库
  6. Unity Standard Assets 简介之 Cameras
  7. 网易2016研发工程师编程题:扫描透镜
  8. Best Time to Buy and Sell Stock
  9. 使用pdb调试python
  10. div在最顶层显示----弹出框效果