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. Access denied for user ''@'localhost' to database 'mysql'

    ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'   在centos下安装好了mysql,用r ...

  2. 用docker弹性部署自己的服务

    很久不看docker的东西了,之前了解的一些基本命令都忘得差不多了,适逢工作需要,再来复习巩固下.今天想完成的是:借助docker不部署下自己的服务. 环境准备 都说“巧妇难为无米之炊”,所以还是需要 ...

  3. BZOJ 4033 [HAOI2015]树上染色 ——树形DP

    可以去UOJ看出题人的题解. 这样的合并,每一个点对只在lca处被考虑到,复杂度$O(n^2)$ #include <map> #include <ctime> #includ ...

  4. leetcode 206 头插法

    头插法,定义temp,Node temp指向每次头结点,Node每次指向要进行头插的节点. 最后返回temp /** * Definition for singly-linked list. * st ...

  5. STL学习笔记(四) 迭代器

    条款26:iterator 优先于 const_iterator, reverse_iterator, const_reverse_iterator iterator, reverse_iterato ...

  6. flask-script插件

    首先在启动Flask项目时,我们可以传不同的参数作为运行参数.但是我们只能在入口app.run()传参.这样十分的不方便.Flask-Script 是一个 Flask 扩展,为 Flask 程序添加了 ...

  7. Python入门--10--序列

    一.与列表.元祖的相同与不同 1.都可以通索引得到元素 2.默认索引从0开始 3.可以通过分片得到一个范围内的元素集合 4.有很多共同的操作符 二. 1.list()这个函数用法 a="we ...

  8. Servlet 2.4 规范之第二篇:Servlet接口

    Servlet接口是Servlet API的最核心抽象类.所有的servlets都直接实现了这个接口,或者以更通用的方式继承了这个接口的实现类.Servlet API自带了两个实现了Servlet接口 ...

  9. 小程序-列表块/类式ul-li格式(1)

    摘要 目前列表能布局出来,但是目前我个人还没解决的问题是:如果每个列表块都有详情页怎么解决呢? 1:我的效果图 2.正常的每个都能点击的html 注:上面的代码确实能够实现我的每个[menu2_vie ...

  10. codeforces Gym 101572 I 有向图最小环路径

    题目链接 http://codeforces.com/gym/101572 题意  一共n个文件  存在依赖关系 根据给出的依赖关系   判断是否存在循环依赖 ,不存在的话输出SHIP IT,存在的话 ...