Groovy私有字段中的错误导致奇怪的行为?

时间:2014-01-27 16:13:58

标签: groovy

似乎将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.

1 个答案:

答案 0 :(得分:3)

默认情况下,EqualsAndHashCode不包含任何私有字段,因此您需要告诉它这样做:

@groovy.transform.EqualsAndHashCode( includeFields=true )