equals
package abstractClasses; import java.time.LocalDate; /**
* Created by xkfx on 2016/12/20.
*/
public class Employee extends Person{
private String name;
private double salary;
private LocalDate hireDay; public Employee(String name, double salary, int year, int month, int day){
super(name);
this.salary = salary;
hireDay = LocalDate.of(year, month, day);
} public double getSalary(){
return salary;
} public LocalDate getHireDay(){
return hireDay;
} public String getDescription(){
return String.format("an employee with a salary of $%.2f", salary);
} public void raiseSalary(double byPercent){
double raise = salary * byPercent / 100;
salary += raise;
} public boolean equals(Object otherObject){
if(this == otherObject)
return true;
// 指向同一个对象就什么事情都没有了。
if(otherObject == null)
return false;
// 所比较对象指向null也不用比了。
if(this.getClass() != otherObject.getClass())
return false;
// 经以上比较可知,两个引用不指向同一个对象,并且两对象属于同类。
Employee other = (Employee)otherObject;
// 经过类型强制转化,才能能够访问otherObject对象具体类的实例域 // 测试实例域是否相同
return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay);
}
}
package abstractClasses; /**
* Created by xkfx on 2016/12/20.
*/
public class Manager extends Employee{
private double bonus; public Manager(String name){
super(name, 0, 2016, 10, 19);
} public boolean equals(Object otherObject){
// 比较父类实例域是否相等
if(!super.equals(otherObject))
return false;
// 比较子类实例域是否相等
Manager other = (Manager)otherObject;
return this.bonus == other.bonus;
}
}
equals的更多相关文章
- equals变量在前面或者在后面有什么区别吗?这是一个坑点
我就不废话那么多,直接上代码: package sf.com.mainTest; public class Test { public static void main(String[] args) ...
- 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 ...
- 【特种兵系列】String中的==和equals()
1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...
- (转)浅谈Java中的equals和==
原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- List<T>Find方法,FindAll方法,Contains方法,Equals方法
假如传入的T是一个类, List<MessageInfos> MessageInfos = new List<MessageInfos>(); MessageInfos= Me ...
- 让代码重构渐行渐远系列(3)——string.Equals取代直接比较与非比较
重构背景及原因 最近由于项目组的人员在不断扩充,导致项目中代码风格各异,大有百花齐放甚至怒放之势.考虑到团队的生存与发展,经过众人多次舌战之后,最终决定项目组根据业务分成几个小分队,以加强团队管理与提 ...
- [java] 更好的书写equals方法-汇率换算器的实现(4)
[java] 更好的书写equals方法-汇率换算器的实现(4) // */ // ]]> [java] 更好的书写equals方法-汇率换算器的实现(4) Table of Content ...
- Equals和ReferenceEquals
稍微分析下一下两个方法的区别: public static bool Equals(object objA, object objB); public static bool ReferenceEqu ...
- 【原创】Java和C#下String类型中的==和equals的原理与区别
一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...
随机推荐
- C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
1.int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型); 2.int.Parse(string sParameter)是个构造函数,参数类型只支持string类型; 3. ...
- C#记录程序耗时的方法
用于准确测量运行时间的方法类: System.Diagnostics.Stopwatch 具体使用方法: using System.Diagnostics; Stopwatch stopwatch = ...
- fight
为啥用block copy. 从栈到拷贝到堆 循环引用,控制器引用block. block里面调用 self self引用控件,控件引用block,block里面引用self http://www. ...
- 如何在Rails中执行Get/Post/Put请求
require 'open-uri' require 'json' require 'net/http' class CoupleController < ApplicationControll ...
- Git stash 常见用法
Git stash git stash这个命令可以将当前的工作状态保存到git栈,在需要的时候再恢复 1.1 git stash 保存当前的工作区与暂存区的状态,把当前的工作隐藏起来,等以后需要的时 ...
- django book querysets
from __future__ import unicode_literals from django.db import models from django.contrib.auth.models ...
- sharding-jdbc-how2work 当当的sharding-jdbc剖析(查询)
1. 以JDBC作为出发点 1.1 重新实现了JDBC的几个接口 实现javax.sql.DataSource接口 ShardingDataSource实现java.sql.Connection接口 ...
- oracle数据导出工具sqluldr2
oracle数据导出工具sqluldr2可以将数据以csv.txt等格式导出,适用于大批量数据的导出,导出速度非常快.导出后可以使用oracle loader工具将数据导入.下载完sqluldr2,工 ...
- SQLite Expert 删除表数据并重置自动增长列
用下面的语句肯定是行不通的,语句不支持 truncate table t_Records 方法:1.删除表数据 2.重置自动增长列 where name='t_Records' /*name :是表名 ...
- Clob类型转化String类型
Clob clob=rs.getClob(列数); Clob clob=rs.getClob("列名");String content=clob.getSubString((lon ...