在 grails 1.1.2 中重写域类的 setter

发布于 2024-08-20 11:43:40 字数 610 浏览 3 评论 0原文

我在 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 行再次调用 setter,而不是分配新值。

有什么提示如何正确覆盖设置器吗?谢谢

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 -> 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-08-27 11:43:40

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

this.myField = val

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

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

this.myField = val

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文