【发布时间】:2014-06-24 17:38:49
【问题描述】:
productTypes
_id = [1, 2, 3]
p.getId()
_id = 1
为什么这个函数总是返回false?例如,如果p.getId() = 1 和productTypes = [1, 2, 3],它应该返回true,但它没有。
List<ProductType> productTypes = new ArrayList<ProductType>();
boolean result = productTypes.contains(p.getId()));
public class ProductType
{
private int _id;
private String _name;
public ProductType(int id)
{
this._id = id;
}
public ProductType(int id,String name)
{
this._id = id;
this._name = name;
}
public int getId()
{
return _id;
}
public void setId(int _id)
{
this._id = _id;
}
public String getName()
{
return _name;
}
public void setName(String _name)
{
this._name = _name;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ProductType other = (ProductType) obj;
if (this._id != other._id)
return false;
return true;
}
}
【问题讨论】: