Groovy子类称为访问闭包的超类方法

Jef*_*rey 3 groovy closures

我有一个groovy超类,看起来像:

class AGroovyClass {
   private String str = "hello"
   void printString(int nTimes) {
     nTimes.times { println str } 
  }        
}
Run Code Online (Sandbox Code Playgroud)

和子类

class AGroovySubclass extends AGroovyClass {
   // some other subclass methods
}
Run Code Online (Sandbox Code Playgroud)

我的客户端代码调用:

new AGroovySubclass().printString(5)
Run Code Online (Sandbox Code Playgroud)

这实际上是因为它说AGroovySubclass没有这样的属性"str"

我会想,因为printString方法在AGroovyClass中,它应该没有问题访问"str"属性,但显然我是不正确的.如果我想保持"str"私有化,那么使这项工作成功的方法是什么?

dvc*_*vcs 5

这是一个私有访问修饰符的旧bug.如果你定义str protected,它可以工作. http://jira.codehaus.org/browse/GROOVY-2433

编辑:你能避免关闭,改为使用for循环吗?不太酷,但工作:)