问题描述
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.
在此链接中,它提到了Groovy中使用的属性和字段的规则。我想应用的应该是最后一个,即元类。不幸的是,我仍然无法理解该行为。
http://groovy.codehaus.org/Groovy+BeansIn 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.
推荐答案
这很可能是比看起来更难修复
This is most probably http://jira.codehaus.org/browse/GROOVY-5776 which is more difficult to fix than it looks like
这篇关于Groovy闭包不适用于超类的静态最终字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!