Groovy父/子私人领域访问古怪与关闭 [英] Groovy Parent/Child Private Field Access Weirdness With Closure

查看:120
本文介绍了Groovy父/子私人领域访问古怪与关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Groovy中,我有一个父类和一个子类,其中父类的构造函数尝试使用闭包来设置父类字段的值,如以下代码所示:

  try {
def x = new ChildClass()
} catch(ex){
ex.printStackTrace(System.err)


class ParentClass {
private values = []

ParentClass(columnCount){
columnCount.times {values.add(' ')}
}
}

class ChildClass extends ParentClass {
ChildClass(){
super(20)
}
}

然而,当我运行这段代码时,我得到一个groovy.lang.MissingPropertyException,没有这样的属性:类的值:ChildClass。但是,如果我将父类的构造函数更改为不使用带有times方法的闭包,如下例所示:

  try {
def x = new ChildClass()
} catch(ex){
ex.printStackTrace(System.err)
}

class ParentClass {
private values = []

ParentClass(columnCount){
def i
for(i = 0; i< columnCount; i ++){
values。 add('')
}
}
}

class ChildClass extends ParentClass {
ChildClass(){
super(20)
}
}

有效。有人可以向我解释这种行为吗?我不明白为什么Groovy认为价值观领域是儿童阶层的财产。我正在使用Groovy版本Groovy版本:1.6.3 JVM:1.5.0_18。

解决方案

当前版本的groovy,并针对在groovy 2.0中修复。请参阅 GROOVY-3073



这是因为元类中的一个范围错误,第一个例子中的闭包无法看到私有类级变量。



这种情况的问题是在超类中声明一个本地别名变量,这将解决闭包中的范围问题。将构造函数更改为:

  ParentClass(columnCount){
def valueAlias = values
columnCount.times {valueAlias.add('')}
}


In Groovy, I have a parent class and a child class where the parent class's constructor tries setting the value of a field of the parent class using a closure as in the following code:

try {
  def x = new ChildClass()
} catch (ex) {
  ex.printStackTrace(System.err)
}

class ParentClass {
  private values = []

  ParentClass(columnCount) {
    columnCount.times { values.add('') }
  }
}

class ChildClass extends ParentClass {
  ChildClass() {
    super(20)
  }
}

However, when I run this code I get a groovy.lang.MissingPropertyException with the message "No such property: values for class: ChildClass". However, if I change the parent class's constructor to not use the closure with the times method as in this example:

try {
  def x = new ChildClass()
} catch (ex) {
  ex.printStackTrace(System.err)
}

class ParentClass {
  private values = []

  ParentClass(columnCount) {
    def i
    for (i = 0; i < columnCount; i++) {
      values.add('')
    }
  }
}

class ChildClass extends ParentClass {
  ChildClass() {
    super(20)
  }
}

It works. Can someone explain this behavior to me? I don't understand why Groovy thinks the values field is a property of the child class. I am using Groovy version "Groovy Version: 1.6.3 JVM: 1.5.0_18".

解决方案

This is a known bug in the current version of groovy and is targeted for being fixed in groovy 2.0. See GROOVY-3073.

It's happening because of a scoping bug in the metaclass where the closure in the first example can't see the private class level variable.

One potential fix that gets around the issue for this situation is to declare a local alias variable in the superclass, this gets around the scoping issue in the closure. Change the constructor to this:

  ParentClass(columnCount) {
     def valueAlias = values
     columnCount.times { valueAlias.add('') }
  }

这篇关于Groovy父/子私人领域访问古怪与关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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