Java的Object的equals方法要求有对称性,为什么我的没有符合对称性但是可以正常运行

/**
*我的t.equals(te)为true

  • te.equals(t)为false
  • 为什么可以正常运行? */ package five;

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

public class Test3 {

public static void main(String[] args) {
      T3 t=new T3();///name="李楠"
      Te3 te=new Te3();////name="刘洋"
      System.out.println(t.equals(te));//true
      System.out.println("==============================");
      System.out.println(te.equals(t));///false
}

}
class T3{
public boolean equals(Object otherObject){
if(this==otherObject){
System.out.println("1");
return true;
}
if(otherObject==null){
System.out.println("2");
return false;
}
if(!(otherObject instanceof T3)){
System.out.println("4");
return false;
}
T3 other=(T3) otherObject;
return name.equals(other.name) && salary==other.salary && hireDay.equals(other.hireDay);
}
private String name="李楠";
private double salary=50000;
private Date hireDay=new GregorianCalendar(1992,3,3).getTime();
}
class Te3 extends T3{
public void s(){
System.out.println("j");
}
public boolean equals(Object otherObject){
if(!super.equals(otherObject)){
System.out.println("5");
return false;
}
if(otherObject instanceof Te3){
Te3 other=(Te3)otherObject;
return name.equals(other.name) && salary==other.salary && hireDay.equals(other.hireDay);
}
else {
return false;
}
}
private String name="刘洋";
private double salary=50000;
private Date hireDay=new GregorianCalendar(1992,3,3).getTime();

}

看看这篇文章吧,折腾了几天。http://blog.csdn.net/u010569227/article/details/10322895

基本类型使用==是等于,引用类型是地址相等。if(this==otherObject)这个不会执行。

T3 other = (T3) otherObject;将Te3转为T3,你的属性全是私有,调用了父类的属性,向上转型后使用同名的方法与属性是使用的父类的方法和属性。

if (!(otherObject instanceof T3))是T3之类的对象,这个一直是false。
if (otherObject instanceof Te3) 子类使用的,otherObject是父类,不是子类型。

name.equals(other.name)。私有属性的不可以这么用!!!!!!

Object类中的euqals方法用来检测一个对象与另一个对象是否相等,其采用的是判断二者是否具有相同的引用,引用相同则一定相等,但是equals方法能够判断引用是否相同来判断比较的对象是否相等, 但不能判断引用不同的对象是否相等 .所以超类中(T3)中有if(this==otherObject)这个方法,用来首先判断两个对象的引用是否相同。