Groovy 中静态闭包的访问值
我想将一些属性存储在静态闭包中,然后在方法调用期间访问它们:
class Person {
static someMap = { key1: "value1", key2: "value2" }
}
那么如何在 Person 中编写一个方法来检索此存储的数据呢?
I'd like to store some properties in a static closure and later access them during a method call:
class Person {
static someMap = { key1: "value1", key2: "value2" }
}
So how can I write a method within Person, which retrieves this stored data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于简单的情况,您最好使用地图。
如果您确实想将其作为闭包进行评估(可能是为了创建您自己的 DSL),那么您需要像 John 指出的那样稍微更改您的语法。这是一种使用 Builder 类来评估传递给构建器的内容中的“某物”闭包的方法。
它使用常规元编程来拦截缺少方法/属性的调用并将其保存下来:
For the simple case, you're better off using a map.
If you really do want to evaluate it as a closure (possibly to create your own DSL), you'll need to change your syntax slightly as John points out. Here's one way to do it using a Builder class to evaluate the "something" closure within whatever is passed to the builder.
It uses groovy metaprogramming to intercept calls with method/property missing and to save them off:
如果它们需要通过闭包而不是映射来初始化,那么就必须有人运行闭包才能拾取并记录设置的值。
你的语法无效。请记住,闭包只是匿名方法。您的语法看起来像是在尝试定义一个映射,但闭包需要调用方法、设置变量或返回映射。例如,
当然,无论是谁运行闭包,都需要有正确的元编程来记录结果。
听起来您真正想要的只是定义一张地图。
If they need to be initialized by a closure, instead of a map, then somebody's got to run the closure in order to pick up and record the values as they're set.
Your syntax there isn't valid. Remember closures are just anonymous methods. Your syntax looks like you're trying to define a map, but closures would need to call methods, set variables or return maps. e.g.
Then of course whoever's running the closure needs to have the right metaprogramming to record the results.
It sounds like what you really want is just to define a map.
这是我想出的来提取静态闭包属性的方法:
This is what I came up with to extract static closure properties: