1:

package com.aff.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));// false--->true 重写equals方法后
}
} class Order {
private int orderId;
private String 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;
} public Order(int orderId, String orderName) {
super();
this.orderId = orderId;
this.orderName = orderName;
} //比较两个order对象的属性是否完全相同,相同的话返回true
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;
}
}
} 输出结果:

false
true

 

2:

package com.aff.equals;

public class TestMyDate {
public static void main(String[] args) {
MyDate m1 = new MyDate(12, 3, 2012);
MyDate m2 = new MyDate(12, 3, 2012); if (m1 == m2) {
System.out.println("m1==m2");
} else {
System.out.println("m1!=m2");
}
// boolean java.lang.Object.equals(Object obj)
// 调用的为Object里面的equals方法,需要重写equals方法
if (m1.equals(m2)) {
System.out.println("m1 is equals m2");
} else {
System.out.println("m1 is not equal to m2");
}
}
} class MyDate {
private int day;
private int month;
private int year; public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
} public int getDay() {
return day;
} public void setDay(int day) {
this.day = day;
} public int getMonth() {
return month;
} public void setMonth(int month) {
this.month = month;
} public int getYear() {
return year;
} public void setYear(int year) {
this.year = year;
} //手动写的
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof MyDate) {
MyDate m = (MyDate) obj;
return this.day == m.day && this.month == m.month && this.year == m.year;
} else {
return false;
}
}
} 输出结果:

m1!=m2
m1 is equals m2

 

最新文章

  1. 两个单选按钮(一个是,一个否 ),一个div层,实现点击隐藏,显示div
  2. mybatis输出SQL
  3. Google Java编程风格指南中文版
  4. 新浪微博授权失败:applications over the unaudited use restrictions
  5. oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
  6. WDC2106 iOS10新特性及开发者要注意什么
  7. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space
  8. Objective-C中的分类与协议
  9. Linux crontab 命令详解(含配置文件路径)
  10. Destoon标签使用技巧十则
  11. 在code first结构下的生成的数据迁移文件,upadate-database失败
  12. ERP服务器简单维护
  13. css入门第一天
  14. Chapter 4 Invitations——9
  15. 解决Javascript中$(window).resize()多次执行(转)
  16. sleep()方法和yield()方法有什么区别?
  17. Vue---从后台获取数据vue-resource的使用方法
  18. re模块逐步进阶
  19. MVC仓储使用join
  20. 45度炸队Alpha冲刺博客集

热门文章

  1. Scrapy爬虫快速入门
  2. 解决虚拟机中linux系统无法使用本机无线wifi联网的问题
  3. 【Kafka】Kafka集群环境搭建
  4. 【Hadoop离线基础总结】HDFS的API操作
  5. 组合模式(c++实现)
  6. .NET Attribute在数据校验上的应用
  7. (2)通信中为什么要进行AMC?
  8. 世界这么大,Python 也想去看看
  9. vue cli脚手架项目利用webpack给生产环境和发布环境配置不同的接口地址或者不同的变量值。
  10. CODING 敏捷实战系列课第五讲:敏捷中国史