equals 来比较两个类指向的内存地址是否一致
package com.zuoye;
public class ceshi {
public static void main(String[] args) {
// TODO Auto-generated method stub
shouji1 sj1 = new shouji1();
shouji1 sj2 = new shouji1();
sj2.jiage = 999;
if(sj1.equals(sj2)){
System.out.println("两个手机是相同的");
}else{
System.out.println("两个手机是不相同的");
}
}
}
package com.zuoye;
public class shouji1 {
float neicun = 2.5f;
float chicun = 5.0f;
int jiage = 999 ;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
shouji1 other = (shouji1) obj;
if (Float.floatToIntBits(chicun) != Float.floatToIntBits(other.chicun))
return false;
if (jiage != other.jiage)
return false;
if (Float.floatToIntBits(neicun) != Float.floatToIntBits(other.neicun))
return false;
return true;
}
}