zoukankan      html  css  js  c++  java
  • equals()方法的运用

    //自定义一个类,比较两个对象的属性值都相等的情况下,返回true的话,就需要重新Object类的equals(Object obj)方法
    //equals()方法的运用
    package exer1; public class TestOrder{ public static void main(String[] args) { Order o1=new Order(111,"跳舞"); Order o2=new Order(111,"跳舞"); System.out.println(o1==o2); System.out.println(o1.equals(o2)); } } class Order { private int orderId; private String orderName; public int getOrderId() { return orderId; } public Order(int orderId, String orderName) { super(); this.orderId = orderId; this.orderName = orderName; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } //比较两个order对象的属性是否完全相同,如果相同返回true,反之false 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(orderName); }else return false; }
    //系统自带的,以后做开发时候直接用

    public boolean equals(Object obj) {
    // if (this == obj)
    // return true;
    // if (obj == null)
    // return false;
    // if (getClass() != obj.getClass())
    // return false;
    // Order other = (Order) obj;
    // if (orderId != other.orderId)
    // return false;
    // if (orderName == null) {
    // if (other.orderName != null)
    // return false;
    // } else if (!orderName.equals(other.orderName))
    // return false;
    // return true;
    // }

    
    }
  • 相关阅读:
    Clipper库中文文档详解
    uboot makefile构建分析
    nvidia tk1使用记录--基本环境搭建
    学习
    es6 es7新语法
    react dva发送请求详解(转)
    antDesign表单getFieldDecorator
    react dav
    js实现截取a标签的href属性和内容
    react学习
  • 原文地址:https://www.cnblogs.com/alhh/p/5384701.html
Copyright © 2011-2022 走看看