在 grails 1.1.2 中重写域类的 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 行再次调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
this
关键字可以避免调用 getter 或 setter:请参阅 http://groovy.codehaus.org/Groovy+Beans#GroovyBeans-Propertyandfieldrules
Use the
this
keyword to avoid calling the getter or setter:See http://groovy.codehaus.org/Groovy+Beans#GroovyBeans-Propertyandfieldrules