5-2 equal getClass or instanceOf

如果是想通过严格的类名判断就使用 getClass

如果是子父类某些属性相等就视为相等就使用instanceOf


/*    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        
        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }
        
        Father father = (Father) obj;
        return (str != null && str.equals(father.str));
    }*/
    
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        
        if (obj == null || !(obj instanceof Father)) {
            return false;
        }
        
        Father father = (Father) obj;
        return (str != null && str.equals(father.str));
    }

 

posted @ 2018-01-09 23:33  aLa神灯  阅读(72)  评论(0编辑  收藏  举报