在下面的代碼片段,我不看好的爲什麼我需要施放?
Product other = (Product)obj;
實用性非常清楚在我看來,這是多餘的。我們可以刪除這個,並將「return this.id == other.id」更改爲「return this.id == obj.id」?
public class Product{
String description;
double price;
int id;
public Product(String d, double p, int i){
description = d;
price = p;
id = i;
}
public boolean equals(Object obj){
if(!(obj instanceof Product){
return false;
}
Product other = (Product)obj;
return this.id == other.id;
}
public int hashcode(){
return id;
}
public String toString(){
return id + " "+description;
}
}
你爲什麼不試試看看會發生什麼? – skaffman