gpt4 book ai didi

java - boolean setter 错误输出

转载 作者:行者123 更新时间:2023-12-02 04:51:05 25 4
gpt4 key购买 nike

当我在 main 方法中调用 BookClass 时,状态的唯一输出是 truefalse。为什么它不会打印出我的 Setter 中的 if-else 语句?

public class Book {

private String title;
private int year;
private String author;
private boolean status;

public Book(String title, int year, String author, boolean status){
this.title = title;
this.year = year;
this.author = author;
this.status = status;
}

public void setStatus(boolean status){
this.status = status;
if(status){
System.out.println("Unavailable");
}
else{
System.out.println("Available");
}
}

最佳答案

您应该在构造函数中使用函数 setStaus,因为 setter 和 getter 不能隐式工作:

public Book(String title, int year, String author, boolean status){
this.title = title;
this.year = year;
this.author = author;
setStatus(status);
}

关于java - boolean setter 错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29234677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com