覆盖grails 1.1.2中域类的setter

时间:2010-02-06 18:52:03

标签: grails dns override gorm setter

我在Grails 1.1.2中有以下两个域类:

class A implements Serializable {
 MyEnumType myField
 Date fieldChanged

 void setMyField(MyEnumType val) {
  if (myField != null && myField != val) {
   myField = val
   fieldChanged = new Date()
  }
 }
}

class B extends A {
 List children
 void setMyField(MyEnumType val) {
  if (myField != null && myField != val) {
   myField = val
   fieldChanged = new Date()
   children.each { child -> child.myField = val }
 }
}

当我设置B实例的myField时,我将setter置于循环中... myField = val line再次调用setter而不是分配新值。

任何提示如何正确覆盖setter?感谢

1 个答案:

答案 0 :(得分:0)

使用this关键字来避免调用getter或setter:

this.myField = val

请参阅http://groovy.codehaus.org/Groovy+Beans#GroovyBeans-Propertyandfieldrules