gpt4 book ai didi

grails - Grails在对象引用上设置元类属性

转载 作者:行者123 更新时间:2023-12-02 14:49:38 25 4
gpt4 key购买 nike

使用Grails域对象,我偶然发现了尝试设置某些属性的问题。

var stepchild=parent.children.find{ it.id==xInt };
stepchild.metaClass.birthMom=biologicalMothersName;

parent.children.each{child->
//when it gets to stepchild no metaclass property birthMom exists....
}

同样,不会在引用的对象上设置自定义错误
var stepchild=parent.children.find{ it.id==xInt };
stepchild.errors.rejectValue('parent',"Not biological parent");

parent.children.each{child->
//when it gets to stepchild no errors....
}

我猜find {}没有返回对父级子项的真实引用?

最佳答案

对于尝试在stepChild对象上设置属性/属性的方式,应使用MetaClass.setAttribute()方法:

stepchild.metaClass.setAttribute(stepChild,'birthMom', biologicalMothersName)

关于grails - Grails在对象引用上设置元类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39678960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com