Groovy闭包不适用于超类的静态最终字段 [英] Groovy closure not work with static final field from super class

查看:54
本文介绍了Groovy闭包不适用于超类的静态最终字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Parent {
    final static String newLine = "*"
}
class Child extends Parent{
    List body = [3, 4, 5]
    String toString() {
        def str = new StringBuilder()
        body.each { str.append(it + newLine) }
        str
    }
}

def c = new Child()
println c

上面是说明问题的一个简单示例。无法在 Eclipse 上使用 Groovy插件进行编译。在超类字段中删除 final static 即可解决此问题。但是,我不知道为什么会这样。

The above is one trivial sample to illustrate the problem. It couldn't be compiled using Groovy plugin on Eclipse. Remove either final or static in the field of super class solves the problem. However, I have no idea why it's the case.

http://groovy.codehaus.org/Groovy+Beans
在此链接中,它提到了Groovy中使用的属性和字段的规则。我想应用的应该是最后一个,即元类。不幸的是,我仍然无法理解该行为。

http://groovy.codehaus.org/Groovy+Beans In this link it mentions the rules for property and fields used in Groovy. I suppose the one applied should be the last one, i.e. meta class. Unfortunately, I still couldn't understand the behavior.

该行为在Groovy的所有版本中都得到了一致的再现。也许有人可以向Groovy团队报告一个错误。我以前从未做过。

The behavior is reproduced consistently in all versions of Groovy. Maybe someone could report one bug to Groovy team. I have never done this before. It would be more efficient if someone experienced could do that.

推荐答案

这很可能是 http://jira.codehaus.org/browse/GROOVY-5776 比看起来更难修复

This is most probably http://jira.codehaus.org/browse/GROOVY-5776 which is more difficult to fix than it looks like

这篇关于Groovy闭包不适用于超类的静态最终字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆