package equals;

public class EqualsTest {
public static void main(String[] args) {
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
Employee alice2 = alice1;
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
Employee bob = new Employee("Bob", 50000, 1989, 10, 1); 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("bob.toString():" + bob.toString()); Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); 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()); }
}
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 name, double salary, int year, int month, int day) {
this.name = name;
this.salary = salary;
hireDay = LocalDate.of(year, month, day);
} public String getName() {
return name;
} public double getSalary() {
return salary;
} public LocalDate getHireDay() {
return hireDay;
} public void raiseSalry(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
} public boolean equals(Object otherObject) {
// a quick test to see if the objects are identical
if (this == otherObject) return true; //must return false if the explict parameter is null
if (otherObject == null) return false; //if the classes don't match, they can't be equal
if (getClass() != otherObject.getClass()) return false; //now we know otherObject is a non-null Employee
Employee other = (Employee) otherObject; //test whether the fields have identical values return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
} public int hashCode() {
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 bouns; public Manager(String name, double salary, int year, int month, int day) {
super(name, salary, year, month, day);
bouns = 0;
} @Override
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bouns;
} @Override
public void raiseSalry(double byPercent) {
super.raiseSalry(byPercent);
} public void setBonus(double bouns) {
this.bouns = bouns;
} public boolean equals(Object otherObject) {
if (!super.equals(otherObject)) return false;
Manager other = (Manager) otherObject;
// super.equals checked that this and other belong to the same class
return bouns == other.bouns;
} public int hashCode() {
return java.util.Objects.hash(super.hashCode(), bouns);
} @Override
public String toString() {
return super.toString() + "[bouns=" + bouns + "]";
}
}
												

object equal的更多相关文章

  1. jdk之object源码理解

    Java所有类都继承与Object,本文谈谈我对object源码的理解,如果有错的,请看官多多批评指正. 1.registerNatives() private static native void ...

  2. equal?, == and eql?, ===,

    1.BasicObject中定义了 == 和equal?这两个方法,两个方法等价,用来比较两个对象是否是同一个对象,是的话结果就为true. 既然两者相同,为何要定义两个呢?只是为了再命名一个别名吗? ...

  3. How to implement equals() and hashCode() methods in Java[reproduced]

    Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...

  4. C++11笔记<一>

    目录: 1.std::share_ptr智能指针: 2.std::tr1::function模板类: 3.stringstream: 4.set/vector/map: 5.static_cast&l ...

  5. 仿网易新闻 ViewPager 实现图片自动轮播

    新闻 App 首页最上方一般会循环播放热点图片,如下图所示. 本文主要介绍了利用 ViewPager 实现轮播图片,图片下方加上小圆点指示器标记当前位置,并利用 Timer+Handler 实现了自动 ...

  6. 每一个C#开发者必须知道的13件事情

    1.开发流程 程序的Bug与瑕疵往往出现于开发流程当中.只要对工具善加利用,就有助于在你发布程序之前便将问题发现,或避开这些问题. 标准化代码书写 标准化代码书写可以使代码更加易于维护,尤其是在代码由 ...

  7. java8 中的时间和数据的变化

    java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b)  排出来的结果是正 ...

  8. C#相等性比较

    本文阐述C#中相等性比较,其中主要集中在下面两个方面 ==和!=运算符,什么时候它们可以用于相等性比较,什么时候它们不适用,如果不使用,那么它们的替代方式是什么? 什么时候,需要自定一个类型的相等性比 ...

  9. JAVA中类、实例与Class对象

    已同步更新至个人blog:http://dxjia.cn/2015/08/java-class-object/ 类 类是面向对象编程语言的一个重要概念,它是对一项事物的抽象概括,可以包含该事物的一些属 ...

随机推荐

  1. 14-new和this

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. fdisk分区自动挂载

    理解/etc/fstab文件配置 首先打开这个文件我们查看下本身内容 vi /etc/fstab   或者   vim /etc/fstab 2 介绍下fstab配置 文件配置每一行属于一个配置,每个 ...

  3. 关于JS中字符串赋值的问题

    JS中不能直接  字符串不能 str[i] = 'x'     不能for循环 字符串length 然后赋值 应该 将字符串转换为数组   而且 字符x[i]=* 不是所有浏览器都兼容的 用  spl ...

  4. 【CCF】通信网络 简单搜索

    去重!不然有环就直接挂掉了...0分 #include<iostream> #include<cstdio> #include<string> #include&l ...

  5. uva 11235 RMQ范围最大值

    题目大意:给一个整数上升序列,对于一系列询问区间(i,j),回答这段区间出现次数最多值所出现的次数. 分析:一个上升序列,相同的值聚集在一起,把相同的值的区间看作一个整体,假设这样的整体有n个,把他们 ...

  6. 【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题

    Tomcat的网站上的说法http://wiki.apache.org/tomcat/FAQ/Logging#Q6: System.out 和 System.err 都被打印到 catalina.ou ...

  7. 实现TTCP (检测TCP吞吐量)

    实现TTCP (检测TCP吞吐量) 应用层协议 为了解决TCP粘包问题以及客户端阻塞问题 设计的应用层协议如下: //告知要发送的数据包个数和长度 struct SessionMessage { in ...

  8. SQL2000数据库密码被替换,重置密码提示未能找到存储过程sp_password解决方案

    利用windows身份验证进入查询分析器后在master数据库下运行如下脚本: create procedure sp_password @old sysname = NULL, -- the old ...

  9. 【kotlin】long转化为date类型 或者date字符串

    1.方法体中的 package org.joda.time.DateTime(long类型) fun Long?.toDateTime() = if (null != this) DateTime(t ...

  10. SSM框架笔记

    配置 Project结构 SpringMVC启用 Spring MVC配置 Spring自己主动扫描 getBean的方法 SpringMVC与Struts2的差别 Log4j 拦截器与过滤器 文件U ...