在类字段中添加private关键字似乎会产生奇怪的行为。使用private关键字时,以下代码会中断,否则会很好。有人可以确认吗?
@groovy.transform.EqualsAndHashCode
class Cell{
private int x; //adding private keyword to the fields causes test to give a false positive
private int y; //adding private keyword to the fields causes test to give a false positive
Cell(_x,_y){
x = _x;
y = _y;
}
}
def liveCells = [] as Set
Cell cell = new Cell(0,0);
Cell diffCell = new Cell(1,1);
liveCells.add(cell)
assert liveCells.contains(cell) == true
assert liveCells.contains(diffCell) == false //test fails due to private keyword to the fields. remove private keywords and the test passes.
默认情况下,EqualsAndHashCode将不包含任何私有字段,因此您需要告诉它这样做:
@groovy.transform.EqualsAndHashCode( includeFields=true )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句