求前辈指点,强制类型转换
package com.Code_Person;import java.util.*;public class Code { public final int id; //身份证号已确认,不能修改 public Code(int id) { this.id=id; } //这里提示我overrides java.lang.Object.equals //问题1:为什么我随便写个名字叫做equals的方法就重载了? public boolean equals(Object anObject) { //问题2:传进来的时候类型和参数不就确定了吗,为什么这里还要判断一次? if(anObject instanceof Object) { //Incompatible operand types int and Code //不兼容的操作数类型int和代码 // return this.id==(Code)anObject.id; //问题3:要改成下面这种写法才没报错 为什么 上面不是已经做了类型转换了吗0.0? Code tmp=(Code)anObject; return this.id==tmp.id; } return false; }}if(anObject instanceof Code)
[解决办法]
this.id==((Code)anObject).id