1

この方法では、2つのセルが等しいかどうかを判断できるようにします。ここで、「等しい」とは、それらが同じ位置にあることを意味します。このコードはinstanceof、オブジェクトが型であることを確認してから型にキャストするためにとキャストのPosition両方を使用して記述しましPositionたが、何らかの理由で機能しないようです。

これが私のコードです:

public class Postion {

    private int column;
    private int row;

    public Position (final int column, final int row)  {
        this.column = column;
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public int getRow() {
        return row;
    }

    public boolean equals(final Object other) {
        if (other instanceof Position) {
            Position position = (Position) other;
            return ((column == other.getColumn()) && (row == other.getRow()));
        } else {
            return false;
        }
    }
}

このエラーコードが表示されます。実際、両方のgetメソッドのエラーコードが表示されます。

error:
cannot find symbol
return ((column == other.getColumn()) && (row == other.getRow()));
^
symbol: method getRow()
location: variable other of type Object
4

3 に答える 3

7
return ((column == other.getColumn()) && (row == other.getRow()));

する必要があります

return ((column == position.getColumn()) && (row == position.getRow()));

getColumn()オブジェクトにはメソッドが含まれていませんgetRow()。これはpositionであるため、そこでpositionを使用する必要があります。

于 2012-12-07T20:02:55.330 に答える
1

入力したPositionオブジェクトの位置の代わりにObjectotherを使用しました。

于 2012-12-07T20:06:37.387 に答える
0

名前を変更する必要があります

Position position = (Position) other;

Position otherPos = (Position) other;

次に使用します

otherPos.getColumn()
于 2012-12-07T20:09:14.710 に答える