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/ 类 类是面向对象编程语言的一个重要概念,它是对一项事物的抽象概括,可以包含该事物的一些属 ...
随机推荐
- DIV垂直/水平居中2(DIV宽度和高度是动态的)
<!doctype html><html><head><meta charset="utf-8"><title>块元素D ...
- 关于IOS项目QQ空间授权提示安装最新版本的QQ的解决方法!
- 【bzoj3123】[Sdoi2013]森林 倍增LCA+主席树+启发式合并
题目描述 输入 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负 ...
- 【Luogu】P1948电话线(二分SPFA)
题目链接 二分最长的电话线长度.把所有大于这个长度的边权设成1,小于等于的设成零,然后跑SPFA看dis[n]是否>k.若>k则l=mid+1 否则r=mid-1 放代码 #include ...
- POJ 1125 Stockbroker Grapevine【floyd】
很裸的floyd #include<cstdio> #include<string.h> #include<algorithm> #define maxn 201 ...
- 【基础操作】FFT / DWT / NTT / FWT 详解
1. 2. 点值表示法 假设两个多项式相乘后得到的多项式 的次数(最高次项的幂数)为 $n$.(这个很好求,两个多项式的最高次项的幂数相加就得到了) 对于每个点,要用 $O(n)$ 的时间 把 $x$ ...
- 关于虚拟机IP网段和公司内网网段的问题?
开发四年只会写业务代码,分布式高并发都不会还做程序员?->>> 请教一个问题,为了解决电脑换网络环境就连不上虚拟机的问题,我虚拟机使用的nat模式,我的VMnet8IP是192 ...
- KVM 网络虚拟化基础
网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源,所以再硬的骨头也必须要把它啃下来. 为了让大家对虚拟化网络的复杂程度有一个直观的认识,请看下图 这是 Open ...
- C#.net的常用函数列表
原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 1、DateTime 数字型 System.DateTime currentTime=new System.Dat ...
- sulime text 常用快捷键总结
Sublime Text 3汉化中文版主要特色: -语法高亮.代码提示补全.代码折叠.自定义皮肤/配色方案.多便签 -代码地图.多种界面布局与全屏免打扰模式 -完全开放的用户自定义配置与神奇实用的编辑 ...