Java学习记 equals()

Java核心技术卷5.2

Object类 所有类的超类
equals方法的重载
equals方法的特性(1.自反性 x.equals(x) true 2.对称性 y.equals(x) true x.equals(y) true 3.传递性 x.equals(y) x.euqals(z) z.equals(y) true4.一般性 反复调用x.equals(y) true 5.对于任何非空引用x,x.equals(null)返回false

equals的实现机制
完美equals方法

public boolean equals(Object otherObject){
    // 对象地址相同,认为一样
    if( this == otherObject) return true;
    //参数为空
    if(otherObject == null) return false;
    //类 型不同
    if(getClass() != otherObject.getClass()) return false; (如果所有的子类有统一的语义 可以使用 instanceof 检测)
    //向下转型     
    Employee other = (Employee)otherObject;
    //要求基本类型域相同   对象域(引用类型)交给java.util.Objects类的equals静态方法
    return Objects.equals(name , other.name)
            && salary == other.salary && Objects.equals(hireDay , other.hireDay);
}

着重讲下向下转型 向上转型
向上转型:
Employee e = new Manager();
向下转型(强制转型):
Manager m =(Manager) e;

String属于引用类型 string对象是不可变的

代码示例

package equals;

import java.time.LocalDate;
import java.util.Objects;

public class Employee {
    private String name;
    private double salary;
    private LocalDate hireDay;

    public Employee(String aName , double aSalary , int year , int month , int day){
        name = aName;
        salary = aSalary;
        hireDay = LocalDate.of(year , month ,day);
    }

    public String getName(){
        return  name;
    }

    public double getSalary(){
        return  salary;
    }

    public LocalDate getHireDay(){
        return hireDay;
    }

    public boolean equals(Object otherObject){
        // 对象地址相同,认为一样
        if( this == otherObject) return true;
        //参数为空
        if(otherObject == null) return false;
        //类 型不同
        if(getClass() != otherObject.getClass()) return false;
        //向下转型
        Employee other = (Employee)otherObject;
        //要求基本类型相同   引用类型交给java.util.Objects类的equals静态方法
        return Objects.equals(name , other.name)
                && salary == other.salary && Objects.equals(hireDay , other.hireDay);
    }

    public int hasCode(){
        return Objects.hash(name , salary , hireDay);
    }

    public String toString(){
        return getClass().getName() + "[name=" + name + "salary=" + salary + "hireDay=" + hireDay + "]";
    }

}
package equals;

public class Manager extends Employee {
    private double bonus;

    public Manager(String name , double salary , int year , int month , int day){
        super(name , salary , year , month ,day);
        bonus = 0;
    }

    public void setBonus(double b){
        bonus = b;
    }

    public double getSalary(){
        double s = super.getSalary();
        return s + bonus;
    }

    public boolean equals(Object otherObject){
        if(!super.equals(otherObject)) return false;
        Manager other = (Manager) otherObject;
        return bonus == other.bonus;
    }

    public int hashCode(){
        return super.hashCode() + 17*new Double(bonus).hashCode();
    }

    public String  toString(){
        return super.toString() + "[bonus=" + bonus + "]";
    }
}
package equals;

public class EqualsT {
    public static void main(String[] args){
        Employee tom1 = new Employee("tom",50000,1999,12,5);
        Employee tom2 = tom1;
        Employee tom3 = new Employee("tom",50000,1999,12,5);
        Employee tom4 = null;
        Manager  tom5 = new Manager("tom",50000,1999,12,5);
        Employee bob = new Employee("bob" ,50000,1998,11,5);

        System.out.println("tom1.equals(tom2):" + tom1.equals(tom2));
        System.out.println("tom1 == tom2:" + (tom1 == tom2));
        System.out.println("tom1 == tom3:" + (tom1 == tom2));
        System.out.println("tom1.equals(bob):" + tom1.equals(bob));
        System.out.println("tom4 == tom1" + (tom4 == tom1));
        System.out.println("tom5.getClsaa == tom1.getClass " + (tom5.getClass() == tom1.getClass()));
        System.out.println(tom1.toString());

        Manager car = new Manager("car" , 80000 , 1995,5,5);
        Manager boss = new Manager("car" , 80000 , 1995,5,5);
        boss.setBonus(5000);
        System.out.println("boss.toString():" + boss.toString());
        System.out.println("car.equals(boss):" + car.equals(boss));
        System.out.println("tom1.hasCode():" + tom1.hasCode());
        System.out.println("tom3.hasCode():" + tom3.hasCode());
        System.out.println(bob.hasCode());
        System.out.println("car.hashCode():" + car.hashCode());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值