Java:if and else 在每个条件下返回 true 或 false



代码:

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof AbstractAnimal))
        return false;
    AbstractAnimal other = (AbstractAnimal) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (sound == null) {
        if (other.sound != null)
            return false;
    } else if (!sound.equals(other.sound))
        return false;
    return true;
}

如果这个表达式最终总是返回 true,它怎么可能有效?我不明白,例如,如果 obj==NULL,它将返回 false,在函数末尾返回 true

return立即从方法返回,执行不会继续到下一个语句。因此,您只从该方法返回一次,您可以忽略后续指令

最新更新