Joh*_*bab 1 java casting instanceof
我有这种方法,我希望能够确定两个单元格是否相等,其中"相等"意味着它们具有相同的位置.我已经编写了这个代码,我使用它们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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误代码,实际上我得到了两个get
方法的错误代码:
error:
cannot find symbol
return ((column == other.getColumn()) && (row == other.getRow()));
^
symbol: method getRow()
location: variable other of type Object
Run Code Online (Sandbox Code Playgroud)
return ((column == other.getColumn()) && (row == other.getRow()));
Run Code Online (Sandbox Code Playgroud)
应该
return ((column == position.getColumn()) && (row == position.getRow()));
Run Code Online (Sandbox Code Playgroud)
对象不包含getColumn()
和getRow()
方法,它是位置,所以你需要在那里使用位置.
归档时间: |
|
查看次数: |
95 次 |
最近记录: |