hashCode 和 equals 方法初探
hashCode 可以翻译为哈希码,或者散列码。应该是一个表示对象的特征值的 int 整数
equals 方法应该用来判断两个对象从逻辑上是否相等
看例程:hashCode 和 equals方法怎么写
看例程:使用equals 用来判断对象是否相等,而非 ==
String 类的 equals 方法
看例程:String 类的 equals 方法和 == 判断
public class ObjectEqualsAppMain {
public static void main(String[] args) {
int defaultCount = 200;
LittleSuperMarket superMarket = new LittleSuperMarket("大卖场",
"世纪大道1号", 500, 600, defaultCount);
MerchandiseV2 t0 = new MerchandiseV2(
"商品" + 0,
"ID" + 0,
defaultCount,
1999,
999
);
MerchandiseV2 t10 = new MerchandiseV2(
"商品" + 10,
"ID" + 10,
defaultCount,
1999,
999
);
MerchandiseV2 t100 = new MerchandiseV2(
"商品" + 100,
"ID" + 100,
defaultCount,
1999,
999
);
// >> TODO 看看结果为什么是这样?
System.out.println(superMarket.findMerchandise(t0));
System.out.println(superMarket.findMerchandise(t10));
System.out.println(superMarket.findMerchandise(t100));
}
}
public class StringEqualsAppMain {
public static void main(String[] args) {
LittleSuperMarket superMarket = new LittleSuperMarket("大卖场",
"世纪大道1号", 500, 600, 100);
String s1 = "aaabbb";
String s2 = "aaa" + "bbb";
// >> TODO 说好的每次创建一个新的String对象呢?
System.out.println("s1和s2用==判断结果:"+(s1 == s2));
System.out.println("s1和s2用 equals 判断结果:"+(s1.equals(s2)));
// >> TODO 打乱Java对String的的优化,再试试看
Scanner scanner = new Scanner(System.in);
System.out.println("请输入s1");
s1 = scanner.nextLine();
System.out.println("请输入s2");
s2 = scanner.nextLine();
System.out.println("s1和s2用==判断结果:"+(s1 == s2));
System.out.println("s1和s2用 equals 判断结果:"+(s1.equals(s2)));
}
}
public class MerchandiseV2 {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
public MerchandiseV2(String name, String id, int count, double soldPrice, double purchasePrice) {
this.name = name;
this.id = id;
this.count = count;
this.soldPrice = soldPrice;
this.purchasePrice = purchasePrice;
}
public MerchandiseV2(String name, String id, int count, double soldPrice) {
// double purPrice = soldPrice * 0.8;
// this(name, id, count, soldPrice, purchasePrice);
this(name, id, count, soldPrice, soldPrice * 0.8);
// double purPrice = soldPrice * 0.8;
}
public MerchandiseV2() {
this("无名", "000", 0, 1, 1.1);
}
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + calculateProfit());
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
// if(profit <= 0){
// return 0;
// }
return profit;
}
public double buy(int count) {
if (this.count < count) {
System.out.println("购买失败,库存不够");
return -1;
}
this.count -= count;
double cost = count * soldPrice;
System.out.println("购买成功,花费为" + cost);
return cost;
}
// >> TODO hashCode 和 equals是我们最常覆盖的两个方法
// >> TODO 覆盖的原则是,equals为true,hashCode就应该相等。这是一种约定俗成的规范
// >> TODO 即equals为true是hashCode相等的充分非必要条件,hashCode相等是equals为true的必要不充分条件
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MerchandiseV2)) return false;
MerchandiseV2 that = (MerchandiseV2) o;
return this.getCount() == that.getCount() &&
Double.compare(that.getSoldPrice(), getSoldPrice()) == 0 &&
Double.compare(that.getPurchasePrice(), getPurchasePrice()) == 0 &&
getName().equals(that.getName()) &&
getId().equals(that.getId());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getId(), getCount(), getSoldPrice(), getPurchasePrice());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getSoldPrice() {
return soldPrice;
}
public void setSoldPrice(double soldPrice) {
this.soldPrice = soldPrice;
}
public double getPurchasePrice() {
return purchasePrice;
}
public void setPurchasePrice(double purchasePrice) {
this.purchasePrice = purchasePrice;
}
}
hashCode 和 equals是我们最常覆盖的两个方法
覆盖的原则是,equals为true,hashCode就应该相等。这是一种约定俗成的规范
即equals为true是hashCode相等的充分非必要条件,hashCode相等是equals为true的必要不充分条件