在下面的代码片段,我不看好的为什么我需要施放?
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