object equal
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的更多相关文章
- jdk之object源码理解
Java所有类都继承与Object,本文谈谈我对object源码的理解,如果有错的,请看官多多批评指正. 1.registerNatives() private static native void ...
- equal?, == and eql?, ===,
1.BasicObject中定义了 == 和equal?这两个方法,两个方法等价,用来比较两个对象是否是同一个对象,是的话结果就为true. 既然两者相同,为何要定义两个呢?只是为了再命名一个别名吗? ...
- 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 ...
- C++11笔记<一>
目录: 1.std::share_ptr智能指针: 2.std::tr1::function模板类: 3.stringstream: 4.set/vector/map: 5.static_cast&l ...
- 仿网易新闻 ViewPager 实现图片自动轮播
新闻 App 首页最上方一般会循环播放热点图片,如下图所示. 本文主要介绍了利用 ViewPager 实现轮播图片,图片下方加上小圆点指示器标记当前位置,并利用 Timer+Handler 实现了自动 ...
- 每一个C#开发者必须知道的13件事情
1.开发流程 程序的Bug与瑕疵往往出现于开发流程当中.只要对工具善加利用,就有助于在你发布程序之前便将问题发现,或避开这些问题. 标准化代码书写 标准化代码书写可以使代码更加易于维护,尤其是在代码由 ...
- java8 中的时间和数据的变化
java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b) 排出来的结果是正 ...
- C#相等性比较
本文阐述C#中相等性比较,其中主要集中在下面两个方面 ==和!=运算符,什么时候它们可以用于相等性比较,什么时候它们不适用,如果不使用,那么它们的替代方式是什么? 什么时候,需要自定一个类型的相等性比 ...
- JAVA中类、实例与Class对象
已同步更新至个人blog:http://dxjia.cn/2015/08/java-class-object/ 类 类是面向对象编程语言的一个重要概念,它是对一项事物的抽象概括,可以包含该事物的一些属 ...
随机推荐
- 07-复习数组和简单api
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HackerRank# Stock Maximize
原题地址 不知道为什么要用动态规划做,明明是扫几遍就行了啊 HackerRank上的题目特别喜欢long long类型啊,不用就爆.. 代码: #include <cmath> #incl ...
- HDU 1693 Eat the Trees ——插头DP
[题目分析] 吃树. 直接插头DP,算是一道真正的入门题目. 0/1表示有没有插头 [代码] #include <cstdio> #include <cstring> #inc ...
- 刷题总结——子串(NOIP2015提高组)
题目: 题目背景 NOIP2015 提高组 Day2 T2 题目描述 有两个仅包含小写英文字母的字符串 A 和 B .现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在 ...
- 【组合数+Lucas定理模板】HDU 3037 Saving
acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...
- 【2018.10.27】CXM笔记
一个数大约有 $O(\sqrt(n)/log^2(n))$ 个约数. 1. 一个棋盘,每个格子最开始都是白的.可以按一个格子,它马跳(日字跳)能到达的 $8$ 个格子反色(当前格不反色).问有多少种方 ...
- git提交之后没有push,代码被覆盖之后恢复
git reflog 通过这个看commit id git reset [commit id] --hard 有时候要删除一个index.lock文件.
- linux虚拟机无法上网 Network is unreachable
系统centos 安装ftp时报错 Couldn't resolve host 'mirrorlist.centos.org [root@wulihua bin]# yum install vsft ...
- 【笔记】Linux内核中的循环缓冲区
1. 有关ring buffer的理解 1) ring buffer位首尾相接的buffer,即类似生活中的圆形跑道: 2) 空闲空间+数据空间=ring buffer大小 3) ring bu ...
- JFinal Weixin 1.6发布【转】
原文:http://www.oschina.net/news/69495/jfinal-weixin-1-6-released#rd 继JFinal 2.1发布之后,再来一发JFinal Weixin ...