开发者

Overriding setter on domain class in grails 1.1.2

I have following two domain classes in 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 开发者_C百科-> child.myField = val }
 }
}

When I set B instance's myField, I get the setter into the cycle... myField = val line calls setter again instead of assiging the new value.

Any hint how to override the setter correctly? Thanks


Use the this keyword to avoid calling the getter or setter:

this.myField = val

See http://groovy.codehaus.org/Groovy+Beans#GroovyBeans-Propertyandfieldrules

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜