API 中的 Object 之 equals 方法
1、== 等于运算符
A:可以比较基本类型:比较的就是值是否相同。
B:可以比较引用类型:比较的就是地址值是否相同。
2、public boolean equals(Object obj)
1)指示其他某个对象是否与此对象“相等”。equals 方法在非空对象引用上实现相等关系。
2)看源码:这个方法,默认情况下,比较的是地址值。
public boolean equals(Object obj) {
return (this == obj) ;
}
3)一般来说,比较地址值得意义不大。所以,需要重写该方法:一般都是用来比较一个类的两个对象的成员变量值是否相等。
4)重写Student类中的equals( ) 方法:
@Override
public boolean equals(Object obj) {
// return super.equals(obj);
// 这里要改进,需要比较对象的成员变量值是否相同!所以,这里比较的就是name和age。但是name是字符串类型,比较不能使用 ==,需要使用String类的equal( )方法:
/* 字符串类的方法:public boolean equals(Object anObject):将此字符串与指定的对象比较,当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
*/
// (1)提高程序的效率:
if(this == obj)
return true;
// (2)提高程序的健壮性:
// 我先判断一下,如果 obj是Student类的对象,可向下转型进行判断,如果不是,直接返回false。
// 这时,我们需要判断的是对象是否是某个类的对象?
// 记住格式: 对象名 instanceof 类名
// 功能:判断该对象名是不是该类名的一个对象。
// (3)最终版的最好使用自动生成的。
if(!(obj instanceof Student)) {
return false;
}
Student ss = (Student)obj;
return this.name.equals(ss.name) && this.age == ss.age;
5)举例子:**************************************************************
package cn.itcast_03; public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub //return super.equals(obj); //这里要改进,要比较对象的成员变量是否相同! //这里比较的就是name和age。 //但是name是字符串类型,比较不能使用==,需要使用String类的equal方法。 /* 字符串类的方法:public boolean equals(Object anObject) * 将此字符串与指定的对象比较。 当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时, 结果才为 true。 */ /* Student ss = (Student)obj; if(this.name.equals(ss.name) && this.age == ss.age) { return true; }else { return false; }*/ // 提高程序的效率 if(this == obj) return true; // 提高程序的健壮性 // 我先判断一下,如果 obj是Student类的对象,可向下转型进行判断,如果不是,直接返回false。 // 这时,我们需要判断的是对象是否是某个类的对象? // 记住格式: 对象名 instanceof 类名 // 功能:判断该对象名是不是该类名的一个对象。 if(!(obj instanceof Student)) { return false; } Student ss = (Student)obj; return this.name.equals(ss.name) && this.age == ss.age; } }*************************************************
package cn.itcast_03; /** public boolean equals(Object obj)比较此对象与指定对象。 当且仅当参数不为 null,并且是一个与该对象包含相同 int 值的 Integer 对象时,结果为 true。 == A:基本类型:比较的就是值是否相同 B:引用类型:比较的就是地址值是否相同 public boolean equals(Object obj):指示其他某个对象是否与此对象“相等”。 equals 方法在非空对象引用上实现相等关系。 //看源码:这个方法,默认情况下,比较的是地址值。 public boolean equals(Object obj) { return (this == obj); } * 一般来说,意义也不大。所有,要重写该方法。一般是用来比较对象的成员是否相等。 * * @author asus * */ public class StudentDemo { public static void main(String[] args) { Student s1 = new Student("qq",11); Student s2 = new Student("ss",22); System.out.println(s1 == s2); //false Student s3 = s1; System.out.println(s1 == s3); //true System.out.println("-----------------"); System.out.println(s1.equals(s2)); //false System.out.println(s1.equals(s3)); //true System.out.println("-----------------"); Student s4 = new Student("cmm",26); Student s5 = new Student("cmm2",26); Student s6 = new Student("cmm2",26); System.out.println(s4.equals(s5)); //false System.out.println(s5.equals(s6)); //true System.out.println("-----------------"); Demo o = new Demo(); System.err.println(s1.equals(o)); } } class Demo { }
2、