首页 > 代码库 > equals();
equals();
public class Testorder {
public static void main(String[] args) {
order o1=new order(1001,"aa");
order o2=new order(1001,"aa");
System.out.println(o1==o2);//false
System.out.println(o1.equals(o2));//true
}
}
class order{
private int orderId;
private String ordername;
public order(int orderId, String ordername) {
super();
this.orderId = orderId;
this.ordername = ordername;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrdername() {
return ordername;
}
public void setOrdername(String ordername) {
this.ordername = ordername;
}
//比较两个order对象的属性是否完全相同
//重写父类的equals方法
public boolean equals(Object obj)
{
if(this==obj)
{
return true;
}
else if(obj instanceof order)
{
order o1=(order)obj;
return this.orderId==o1.orderId
&& this.ordername.equals(o1.ordername);
}
else
{
return false;
}
}
}
equals();