在阅读 Benjamin J. Evans 和 Martijn Verburg 所著的“The Well-Grounded Java Developer”一书时,我在 Groovy 中遇到了以下示例:
class Character {
private int strength
private int wisdom
}
def pc = new Character(strength: 10, wisdom: 15)
pc.strength = 18
println "STR [" + pc.strength + "] WIS [" + pc.wisdom + "]"
代码片段的输出如下:
STR [18] WIS [15]
到现在为止没有任何问题。上面的代码已经用
@Immutable
稍作修改注释,正如书中所建议的:import groovy.transform.*
@Immutable
class Character {
private int strength
private int wisdom
}
def pc = new Character(strength: 10, wisdom: 15)
pc.strength = 18
println "STR [" + pc.strength + "] WIS [" + pc.wisdom + "]"
最后一个片段的输出仍然相同:
STR [18] WIS [15]
预期的结果是什么,但不像上面的那个......
为什么不可变类的对象看起来像可变类? Groovy 中的不变性概念是否允许修改类字段?
最佳答案
为了解决您报告的问题很简单,即删除 access modifier to fields
.那应该可以解决问题。
import groovy.transform.Immutable
@Immutable
class Character {
int strength
int wisdom
}
def pc = new Character(strength: 10, wisdom: 15)
pc.strength = 18
println "STR [" + pc.strength + "] WIS [" + pc.wisdom + "]"
现在,它将引发异常,如下所示:
groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: strength for class: Character
at Character.setProperty(Script1.groovy)
at Script1.run(Script1.groovy:10)
如果您想了解有关行为不同原因的更多详细信息,请参阅 here
关于groovy - 为什么不可变类在 Groovy 中是可变的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449224/