重写equals方法---java
/**
* 重写equals方法
*/
public class Animal {
String color;
private int age;
public boolean equals(Object obj){
Animal other = (Animal)obj;
//如果参数是null,直接返回false
if (obj==null){
return false;
}
//如果两个变量指向同一个空间,直接返回false
if (this==obj){
return true;
}
if (this.color.equals(other.color)&&this.age== other.age){
return true;
}else{
return false;
}
}
}
public class Dog extends Animal{
private String nikeName;
private String type;
public boolean equals(Object obj){
Dog other = (Dog)obj;
boolean flag = super.equals(obj);
if (!flag){
return false;
}else{
if (this.nikeName.equals(other.nikeName)&&this.type.equals(other.type)){
return true;
}else{
return false;
}
}
}
}