对不对啊?
package com.Imooc01;
public class Initail {
public static void main(String[] args) {
Iphone iphone6=new Iphone();
Iphone iphone6s=new Iphone();
if(iphone6.equals(iphone6s)){
System.out.println("两个对象是相同的");
}else{
System.out.println("两个对象是不相同的");
}
}
}
package com.Imooc01;
public class Telphone {
float screen=5.0f;
float cpu=1.4f;
float mem=2.0f;
}
package com.Imooc01;
public class Iphone extends Telphone {
float screen=5.0f;
float cpu=1.4f;
float mem=2.0f;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Float.floatToIntBits(cpu);
result = prime * result + Float.floatToIntBits(mem);
result = prime * result + Float.floatToIntBits(screen);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Iphone other = (Iphone) obj;
if (Float.floatToIntBits(cpu) != Float.floatToIntBits(other.cpu))
return false;
if (Float.floatToIntBits(mem) != Float.floatToIntBits(other.mem))
return false;
if (Float.floatToIntBits(screen) != Float.floatToIntBits(other.screen))
return false;
return true;
}
}
equals只能比较值是否相等。“==”是比较内存是不是相等,也可用作判断是否指示同一对象
浮点数的比较 你这样可以 也可以使用浮点数相减小于某一个精度 也可以