当我在main方法中调用BookClass
时,状态的唯一输出是true
或false
。
为什么不打印我的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");
}
}
答案 0 :(得分:2)
您应该在构造函数中使用函数setStaus
,因为setter和getter不能隐式工作:
public Book(String title, int year, String author, boolean status){
this.title = title;
this.year = year;
this.author = author;
setStatus(status);
}