尽管在我的 setter 中满足 if 条件,但为什么我的布尔值仍然为 false?

Why does my boolean value remain as false despite meeting the if condition in my setter?

public class Main {
    public static void main(String[] args) {
        // test1 where all variables are valid
        Main test1 = new Main("David", "male", 2);
        System.out.println(test1);
    }
    private String name;
    private String gender;
    private int noOfEyes;
    private boolean human;

    public Main(){
        setHuman(false);
    }

    public Main(String isName, String isGender, int isNoOfEyes){
        setName(isName);
        setGender(isGender);
        setNoOfEyes(isNoOfEyes);
    }
    public String getName(){
        return name;
    }
    public String getGender(){
        return gender;
    }
    public int getNoOfEyes(){
        return noOfEyes;
    }
    public boolean getHuman(){
        return human;
    }

    public boolean setName(String newName){
        boolean flag =false;
        if(newName.length() > 1){
            name = newName;
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }
    public boolean setGender(String newGender){
        boolean flag =false;
        if(newGender.length() > 1){
            gender = newGender;
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }
    public boolean setNoOfEyes(int newNoOfEyes){
        boolean flag =false;
        if(newNoOfEyes == 2){
            noOfEyes = newNoOfEyes;
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }
    public boolean setHuman(boolean newHuman) {
        boolean flag = false;
        if(name == null || gender == null || noOfEyes == 0) {
            human = newHuman;
            flag = true;
        }
        else {
            flag = false;
        }
        return flag;
    }
    public String toString(){
        String output = "";
        output += "Name is: " + getName() + "\n";
        output += "Gender is: " + getGender() + "\n";
        output += "No of eyes is: " + getNoOfEyes() + "\n";
        output += "Is Human?: " + getHuman() +"\n";

        return output;

    }

}

我知道布尔实例变量会自动实例化。作为假。但是由于满足 setHuman if 条件,因为名称不为空,性别不为空且 noOfEyes 不为 0, 布尔人类值不应该变为真吗?

布尔实例变量不在构造函数中,因为不允许我这样做

正如您在问题中指出的,booleans 默认为 false。这包括布尔值 human,它未在您的三参数构造函数中设置,因此它使用默认值。

但是你为什么要从你的 setter 返回 booleans?您的 class 定义不是很传统。为什么不对您的字段执行以下操作。

public void setGender(String gender) {
    this.gender = gender;
}
public String getGender() {
    return gender;
}

public void setHuman(boolean value) {
     this.human = human;
}   

public boolean isHuman() {
   return human;
}    

由于您可能是 Java 的新手,更好的性别解决方案是建立 enum 允许的性别。这将确保无法传递 treetable 之类的内容(如果您使用字符串,则可以)。以下是它的工作原理。

enum Gender {MALE, FEMALE, TRANS, NONBINARY} // etc.
private Gender gender; // class field
public void setGender(Gender gender) {
    this.gender = gender;
}

public Gender getGender() {
    return gender;
}

例如,将其设置为男性,您可以这样做。

setGender(Gender.MALE);