5
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

以上是说明问题的一个简单示例。它无法使用Groovy pluginon编译Eclipse。删除final或者static在领域中的超类解决了这个问题。但是,我不知道为什么会这样。

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

该行为在所有版本的 Groovy 中都一致地重现。也许有人可以向 Groovy 团队报告一个错误。我以前从来没有这样做过。如果有经验的人能做到这一点会更有效率。

4

2 回答 2

2

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

于 2013-02-01T18:56:14.543 回答
1

正如 blackdrag 已经指出的那样:这是一个错误。但另一种解决方法是添加protected关键字:

protected final static String newLine = "*"
于 2013-12-09T14:14:18.920 回答