equals方法
equals() 方法用于比较两个对象是否相等,它有以下特征:
- 是一个方法,非运算符
- equals() 方法只能适用于引用数据类型。
- object 类中定义的 equals() 方法和==的作用是相同的,比较两个对象的地址值。
==符号使用时,两边的类型可以不同,equals() 两个对象的类型要相同,否则结果为 false
public boolean equals(object obj){
return (this == obj);
}
//object类中定义的equals()方法和==的作用是相同的,比较两个对象的地址值
public class Demo01 {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student();
Student student3 = student2;
System.out.println(student1.equals(student2));
System.out.println(student2.equals(student3));
}
}
false
true
- 像 String、Date、File、包装类 等都重写了 Object 类中的 equals() 方法,重写以后,比较的不是两个对象地址是否相同,而是比较两个对象的“实体内容”是否相同。
public class Demo01 {
public static void main(String[] args) {
Date date1 = new Date(1000);
Date date2 = new Date(1000);
Date date3 = new Date(-1000);
System.out.println(date1.equals(date2));
System.out.println(date1.equals(date3));
}
}
true
false
- 通常情况下,自定义的类使用 equals() 的话,也通畅比较的是两个对象的“实体内容”是否相同,那么则需要对 Object 类当中的 equals() 进行重写。
重写原则:比较两个实体的内容是否相同,如下:Student 的 age 和 name
//Student重写equals()方法
public class Student {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//重写原则:比较两个对象的实体内容(即:name和age)是否相同
@Override
public boolean equals(Object obj) {
if (this== obj) {//判断地址是否相同,如果相同,直接return true
return true;
}
if(obj instanceof Student) {
Student student = (Student)obj;
//比较两个对象的每个属性都相同。
// if(this.age == student.age && this.equals(student.name)) {
// return true;
// }else {
// return false;
// }
//或
return (this.age == student.age && this.name.equals(student.name));
}else
return false;
}
}
//测试类
public class equals_test {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.setAge(15);
s1.setName("DuoLaAMeng");
s2.setAge(16);
s2.setName("DaXiong");
s3.setAge(16);
s3.setName("DaXiong");
System.out.println(s2.equals(s1));
System.out.println(s2.equals(s3));
}
}
public class Customer {
//自定生成equals()方法
//菜单栏Source---->Generate hashCode() and equals()...
private int age;
private String name;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
练习1:
/*
* 编写Order类,有int型的orderId,String类型的orderName
* 相应的get/set方法和构造函数
* 重写父类equals()方法,public boolean equals(object obj)
* 判断测试类中创建的两个对象是否相等。
*/
public 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;
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj instanceof Order) {
Order order = (Order)obj;
return this.orderId==order.orderId && this.orderName.equals(order.orderName);
//String 的内容存在常量池当中,常量的特点是若新定义的变量更已有的同,就直接复用,地址是一样的。
//故,有的时候,this.orderName.equals(order.orderName)换成this.orderName = order.orderName
//但是不能使用==,因为orderName 的 String 类型不一定是从一个常量池来的。只要用到引用类型,就要用equals。
}else {
return false;
}
}
}
//测试类
public class orderText {
public static void main(String[] args) {
Order order1 = new Order(1,"A");
Order order2 = new Order(2,"B");
System.out.println(order1.equals(order2));//false
Order order3 = new Order(2,"B");
System.out.println(order2.equals(order3));//true
}
}
练习2:
import java.util.Objects;
public class MyDate {
private int day;
private int month;
private int 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 MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj instanceof MyDate){
MyDate myDate=(MyDate)obj;
return this.day==myDate.day
&& this.month==myDate.month
&&this.year==myDate.year;
}else {
return false;
}
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// MyDate myDate = (MyDate) o;
// return day == myDate.day &&
// month == myDate.month &&
// year == myDate.year;
// }
}
//测试类:
public class myDateText {
public static void main(String[] args) {
MyDate m1 = new MyDate(14, 3, 1976);
MyDate m2 = new MyDate(14, 3, 1976);
if (m1 == m2) {
System.out.println("m1==m2");
} else {
System.out.println("m1!=m2"); // m1 != m2
}
if (m1.equals(m2)) {
System.out.println("m1 is equal to m2");// m1 is equal to m2
} else {
System.out.println("m1 is not equal to m2");
}
}
}
m1!=m2
m1 is equal to m2