equals方法

longer5153458 2013-08-27 11:32:21
package next11;

import java.util.Date;
import java.util.GregorianCalendar;



public class EmployeeTest {
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 Brandson", 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("alice1 equals(bob) "+alice1.equals(bob));

System.out.println("bob.toString "+bob);



}
}

class Employee{
private String name;
private double salary;
private Date hireDay;
public Employee(String n,double s,int year,int month,int day){
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month -1,day);
hireDay = calendar.getTime();
}
public Date getHireDay() {
return hireDay;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void raiseSlary(double byPercent){
double raiseSalary = salary*byPercent/100;
salary +=raiseSalary;
}

public boolean equals (Object otherObject){
if(this == otherObject) return true;//这一句要怎么理解?本类对象与otherObject引用的对象一致?alice1.equals(alice3)套上的话又该怎么具体分析呢?
//Object otherObject = alice3?如果是这样,所指的对象应该不同呀。有点卡住了,呵呵
if(otherObject==null) return false;
if(this.getClass()!=otherObject.getClass()) return false;
Employee other =(Employee)otherObject;
return name.equals(other.name)
&&salary == other.salary
&&hireDay.equals(other.hireDay);
}

public int hashCode(){
return 7*name.hashCode()
+11*new Double(salary).hashCode()
+13*hireDay.hashCode();
}

public String toString(){
return getClass().getName()
+"[name="+name
+",salary="+salary
+",hireDay="+hireDay
+"]";
}

}

class Manager extends Employee{
private double bonus;
public Manager(String n,double s,int year,int month,int day){
super(n,s,year,month,day);
bonus = 0;
}

public double getSalary() {
double baseSalary = super.getSalary();
return super.getSalary()+baseSalary;
}

public void setBonus(double b){
bonus =b;
}

public boolean equals(Object otherObject){
if(!super.equals(otherObject)) return false;
Manager other = (Manager)otherObject;
return bonus == other.bonus;
}

public int hashCode(){
return super.hashCode()
+17*new Double(bonus).hashCode();
}

public String toString(){
return super.toString()
+"[bonus="+bonus
+"]";
}




}
...全文
178 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Logical_butterfly 2013-08-28
  • 打赏
  • 举报
回复
那句相当于本类引用对象和参数引用对象哈希码相同,也就是地址相同,指向同一个对象实例 重写的equals方法是先比较引用对象alice1与alice3的哈希码,如果相同返回true,如果不同再继续比较他们的对象实例,实例相同也返回true
longer5153458 2013-08-28
  • 打赏
  • 举报
回复
引用
那句相当于本类引用对象和参数引用对象哈希码相同,也就是地址相同,指向同一个对象实例 重写的equals方法是先比较引用对象alice1与alice3的哈希码,如果相同返回true,如果不同再继续比较他们的对象实例,实例相同也返回true
"=="不是要求所引用的对象必须是同一个才是true么? if(this == otherObject) return true;我是这么理解的:Object otherObject = alice3;(otherObject引用alice3的对象)然后alice1=otherObject(很明不是同一对象)所以应该返回false。但这是错的,我就是不知道我错在哪里?
oh_Maxy 2013-08-28
  • 打赏
  • 举报
回复
equals有如下特性: 1. 自等(就是自己equals自己,结果为true,你的疑问解决了吧:this表示本对象) 2. 对称(a equals b,那么b equals a) 3. 传递(a equals b, b equals c, 那么a equals c) 4. 根据惯例,还有个非空(不能与null相等)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧