instanceof 看左边的对象是否是右边类的实例
举例子:可以说我是一个人,但是不能说人都是我
public class Father {
public void smoke(){
System.out.println("我爱抽烟");
}
protected String hobby;
public String getHobby() {
return hobby;
}
public Father(String hobby) {
this.hobby = hobby;
}
// 重写equals方法
@Override
public boolean equals(Object obj) {
Father father;
if (obj instanceof Father){
father = (Father)obj;
if(this.getHobby().equals(father.getHobby())){
return true;
}
}
return false;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
使用重写的equals方法(instanceof)
System.out.println(father1.equals(father2));
结果: