作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行下面的代码片段,但不确定如何解决访问返回闭包的类的静态方法
内的tr
变量。
tr = 'GROOVY'
class tmp {
static map = {
def cols = [ header: { "JAVA" }, trailer: { tr } ]
}
}
tmp.map().collect { k,v -> println v()}
运行时抛出以下错误。
JAVA
Caught: groovy.lang.MissingPropertyException: No such property: tr for class: tmp
Possible solutions: map
groovy.lang.MissingPropertyException: No such property: tr for class: tmp
Possible solutions: map
at tmp$__clinit__closure1$_closure3.doCall(test.groovy:31)
at tmp$__clinit__closure1$_closure3.doCall(test.groovy)
at test$_run_closure1.doCall(test.groovy:35)
at test.run(test.groovy:35)
[Finished in 2.0s with exit code 1]
最佳答案
您可以使用Groovy Binding class目的。它用于将值传入和传出 Groovy 脚本。默认情况下,有一个可用于脚本的易于使用的绑定(bind)对象。在类外部声明的属性会自动添加到该对象中。
tr = 'GROOVY'
class Tmp {
static Binding context
static map = {
def cols = [header: { "JAVA" }, trailer: { context.tr }]
}
}
Tmp.context = binding
Tmp.map().collect { k, v -> println v() }
最好创建自己的绑定(bind)对象(或者我应该说上下文),而不是依赖默认对象,如下所示:
def myBinding = new Binding(['tr': 'Groovy'])
class Tmp {
static Binding context
static map = {
def cols = [header: { "JAVA" }, trailer: { context.tr }]
}
}
Tmp.context = myBinding
Tmp.map().collect { k, v -> println v() }
关于java - Groovy:访问静态类闭包内的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57501709/
我正在尝试对 StockData 表执行 OHCL sql 查询 (SQL Server 2012)。每天有数千行添加到表中,我想获取每天的开盘价、最高价、最低价和收盘价数据。 建表sql如下:
我是一名优秀的程序员,十分优秀!