image.png

    1. package com.atguigu.exercise3;
    2. public class Order {
    3. //属性
    4. private int orderId;
    5. private String orderName;
    6. //构造器
    7. public Order(){
    8. }
    9. public Order(int orderId,String orderName){
    10. this.orderId = orderId;
    11. this.orderName = orderName;
    12. }
    13. //方法
    14. public int getOrderld() {
    15. return orderId;
    16. }
    17. public void setOrderld(int orderId) {
    18. this.orderId = orderId;
    19. }
    20. public String getOrderName() {
    21. return orderName;
    22. }
    23. public void setOrderName(String orderName) {
    24. this.orderName = orderName;
    25. }
    26. public boolean equals(Object obj){
    27. if(this == obj){
    28. return true;
    29. }
    30. if(obj instanceof Order){
    31. Order order = (Order)obj;
    32. return this.orderId == order.orderId && this.orderName.equals(order.orderName);
    33. }
    34. return false;
    35. }
    36. }

    1. package com.atguigu.exercise3;
    2. public class OrderTest {
    3. public static void main(String[] args) {
    4. Order order1 = new Order(1001,"AA");
    5. Order order2 = new Order(1001,"BB");
    6. System.out.println(order1.equals(order2));
    7. Order order3 = new Order(1001,"BB");
    8. System.out.println(order2.equals(order3));
    9. }
    10. }