我是 java 新手,我一直在做这个练习,但一直收到错误:不能取消引用 int。我看到了几个类似的问题,但仍然无法弄清楚我自己的情况。这是完整的代码:
package inclass;
class OneInt {
int n;
OneInt(int n) {
this.n = n;
}
@Override public boolean equals(Object that) {
if (that instanceof OneInt) {
OneInt thatInt = (OneInt) that;
return n.equals(thatInt.n); // error happens here
} else {
return false;
}
}
public static void main(String[] args) {
Object c = new OneInt(9);
Object c2 = new OneInt(9);
System.out.println(c.equals(c2));
System.out.println(c.equals("doesn't work"));
}
}
非常感谢你帮我解决了这个小麻烦。