在 Constructor 中设置一个布尔值,在一个方法中更改它,return 在另一个方法中

Setting a boolean value in Constructor, changing it in a method, return it in another

我还是新手,所以请随意指责我问傻xD。我刚开始编码。所以我想具体说明我的问题,让你清楚。我对此感到困惑:我们需要一个将我们的值设置为 false 的构造函数 (public DoggoII)。然后我们需要一个方法 (makeGoodBoi()) 将值设置为 true 然后我需要另一个方法 (isGoodBoi()) 来 return 私有字段 goodBoi 的值和 System.out.print 一些东西。将其余代码视为已完成。有人可以给我提示或提示吗?因为我有点迷路了。

问题是如果我有一个我找不到的错误,以及如何 return 通常在另一种方法中的布尔值。谢谢!

public class Doggo {
private String name;
private boolean goodBoi;

public Doggo(String name){
    this.name = name;
}

public String getName() {
    return name;
}

public void makeBark() {
    System.out.println(name + " said: Woof woof");
}


public Doggo (boolean goodBoi){
    this.goodBoi= false;
}

public void makeGoodBoi(){
    this.goodBoi = !this.goodBoi;
}

public void isGoodBoi(){
    if (makeGoodBoi()){
        return;
    }
}

public void whosAGoodBoi() {
    if (isGoodBoi()) {
        System.out.println(name + " is such a good boii");
    } else {
        System.out.println(name + " is not a good boi :(");
    }
}


public static void main(String[] args) {
    Doggo dog = new Doggo("Casper");
    System.out.println(dog.getName());
    dog.makeBark();
    
}

}

只是一个基本的 getter,使用 boolean 作为 return 类型而不是 void

public boolean isGoodBoi() {
    return goodBoi;
}

因为 goodBoi 是一个 class 成员并且默认情况下布尔值 class 成员是假的,所以除了添加 getter

public boolean isGoodBoi() {
       return goodBoi;
}

这将发送 class 成员的任何当前值。

所以得到这个就像;

DOGGO_OBJECT.isGoodBoi();

Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later.

public void makeGoodBoi() {
 this.goodBoi = true;
}