package top.qaqaq.java.P299;
public class OdrerTest {
public static void main(String[] args) {
Order order1 = new Order(1001, "AA");
Order order2 = new Order(1001, "BB");
System.out.println(order1.equals(order2));
Order order3 = new Order(1001, "BB");
System.out.println(order2.equals(order3));
// String s1 = "BB";
// String s2 = "BB";
// System.out.println(s1 == s2);//true
}
}
class Order {
private int orderld;
private String orderName;
public Order(int orderld, String orderName) {
super();
this.orderld = orderld;
this.orderName = orderName;
}
public int getOrderld() {
return orderld;
}
public void setOrderld(int orderld) {
this.orderld = orderld;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj instanceof Order) {
Order order = (Order)obj;
//正确的:
return this.orderld == order.orderld &&
this.orderName.equals(order.orderName);
//错误的:
// return this.orderld == order.orderld &&
// this.orderName == order.orderName;
}
return false;
}
}
暂无评论