栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 核心技术(第八版)卷1:基础知识:例5-3P168

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 核心技术(第八版)卷1:基础知识:例5-3P168

import java.util.*;
//本程序演示的是:Equals() 方法 ,hashcode()方法,toString() 方法
public class EqualsTest
{
    public static void main(String [] args)
    {
        Employee alice1=new Employee("Alice Adams",75000,2010,12,15);
        Employee alice2=alice1;
        Employee alice3=new Employee("Alice Adams",75000,2010,12,15);
        Employee bob=new Employee("Bob Dyln",50000,2010,10,15);
        System.out.println("alice1==alice2:"+(alice1==alice2));

        System.out.println("alice1==alice3:"+(alice1==alice3));

        System.out.println("alice1.equals(alice3):"+alice1.equals(alice3));

        System.out.println("alice1.equals(bob):"+alice1.equals(bob));

        System.out.println("bob.toString():"+bob);

        Manager carl=new Manager("SpringLee",100000,2022,5,6);
        Manager boss=new Manager("SpringLee",100000,2022,5,6);
        boss.setBonus(5000);

        System.out.println("boss.toSTring():"+boss);

        System.out.println("carl.equals(boss):"+carl.equals(boss));

        System.out.println("alice1.hashCode():"+alice1.hashCode());

        System.out.println("alice3.hashCode():"+alice3.hashCode());

        System.out.println("bob.hashCode():"+bob.hashCode());

        System.out.println("carl.hashCode():"+carl.hashCode());
    }
}
class Employee
{
    public Employee(String n,double s,int year,int month,int day)
    {
        name=n;
        salary=s;
        GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
        hireDay=calendar.getTime();
    }
    public String getName()
    {
        return name;
    }
    public double getSalary()
    {
        return  salary;
    }
    public Date getHireDay()
    {
        return hireDay;
    }
    public void raiseSalary(double byPercent)
    {
        double raise=salary*byPercent/100;
        salary+=raise;
    }
    public boolean equals(Object otherObject)
    {
        //快速测试 是否对象是同一个对象
        if(this==otherObject) return true;
        //如果传入的参数是空的,那么就返回false
        if(otherObject==null)return false;
        //如果类都不匹配,那么他们不可能相等
        if(getClass()!=otherObject.getClass())return false;
        //现在我们知道了otherObject是一个非空的Employee
        Employee other=(Employee) otherObject;
        //测试是否字段的值也是相等的
        return name.equals(other.name) && salary==other.salary && hireDay.equals(other.hireDay);
    }

    public int hashCode()
    {
        return 7*name.hashCode()+11* new Double(salary).hashCode()+13*hireDay.hashCode();
    }
    public  String toString()
    {
        return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
    }
    private String name;
    private double salary;
    private Date hireDay;
}
class Manager extends Employee
{
    
    public Manager(String n,double s,int year,int month,int day)
    {
        super(n,s,year,month,day);
        bonus=0;
    }
    public  double getSalary()
    {
        double baseSalary=super.getSalary();
        return baseSalary+bonus;
    }
    public void setBonus(double b)
    {
        bonus=b;
    }
    public  boolean equals(Object otherObject)
    {
        if(!super.equals(otherObject))return false;
        Manager other=(Manager) otherObject;
        //super.equals 检查 这个和另外的对象 是属于同一个类
        return bonus==other.bonus;
    }
    public  int hashCode()
    {
        return super.hashCode()+17*new Double(bonus).hashCode();
    }
    public String toString()
    {
        return super.toString()+"[bonus="+bonus+"]";
    }
    private double bonus;
}

运行结果:

C:Users86133.jdksopenjdk-18.0.1binjava.exe "-javaagent:D:Program FilesideaIC-2022.1.winlibidea_rt.jar=16282:D:Program FilesideaIC-2022.1.winbin" -Dfile.encoding=UTF-8 -classpath D:eCodeJavaCoreJavaVolumeI_FundamentalsExample5-3EqualsTestAgainoutproductionExample5-3EqualsTestAgain EqualsTest
alice1==alice2:true
alice1==alice3:false
alice1.equals(alice3):true
alice1.equals(bob):false
bob.toString():Employee[name=Bob Dyln,salary=50000.0,hireDay=Fri Oct 15 00:00:00 CST 2010]
boss.toSTring():Manager[name=SpringLee,salary=100000.0,hireDay=Fri May 06 00:00:00 CST 2022][bonus=5000.0]
carl.equals(boss):false
alice1.hashCode():337832952
alice3.hashCode():337832952
bob.hashCode():638954909
carl.hashCode():-209241639

进程已结束,退出代码0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/852306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号