Java 重写equals

 1 package com.bytezreo.objectclass;
 2 /**
 3  * 
 4  * @Description   重写equals
 5  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 6  * @version
 7  * @date 2021年9月24日下午1:03:12
 8  * @
 9  *
10  */
11 
12 
13 public class OrderEquals 
14 {
15     public static void main(String[] args) {
16         
17         OrderTwo order1 = new OrderTwo(1001,"Tom");
18         OrderTwo order2 = new OrderTwo(1001,"Tom");
19         
20         OrderTwo order3 = new OrderTwo(1001,"jon");
21         
22         boolean isEquals = order1.equals(order2);
23         System.out.println("order1 与 order2 是否相等:"+isEquals);
24         
25         boolean isEquals2 = order1.equals(order3);
26         System.out.println("order1 与 order3 是否相等:"+isEquals2);
27     }
28 }
29 
30 
31 class OrderTwo
32 {
33     private int orderId;
34     private String orderName;
35     
36     
37     
38     public int getOrderId() 
39     {
40         return orderId;
41     }
42     public void setOrderId(int orderId)
43     {
44         this.orderId = orderId;
45     }
46     public String getOrderName()
47     {
48         return orderName;
49     }
50     public void setOrderName(String orderName)
51     {
52         this.orderName = orderName;
53     }
54     
55     public OrderTwo(int orderId,String orderName)
56     {
57         super();
58         this.orderId = orderId;
59         this.orderName =orderName;
60     }
61 
62     @Override
63     public boolean equals(Object obj) 
64     {
65         if(this == obj)
66         {
67             return true;
68         }
69         if(obj instanceof OrderTwo)
70         {
71             OrderTwo order =(OrderTwo)obj;
72             
73             return this.orderId == order.orderId && 
74                     this.orderName.equals(order.orderName);
75             
76         }
77         
78         return false;
79         
80     }
81     
82     
83 }

 

posted on 2021-09-24 13:33  Bytezero!  阅读(44)  评论(0)    收藏  举报