Boolean setter 错误输出
Boolean setter wrong output
当我在 main 方法中调用 BookClass
时,status 的唯一输出是 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");
}
}
您应该在构造函数中使用函数 setStaus
,因为 setter 和 getter 不会隐式工作:
public Book(String title, int year, String author, boolean status){
this.title = title;
this.year = year;
this.author = author;
setStatus(status);
}
当我在 main 方法中调用 BookClass
时,status 的唯一输出是 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");
}
}
您应该在构造函数中使用函数 setStaus
,因为 setter 和 getter 不会隐式工作:
public Book(String title, int year, String author, boolean status){
this.title = title;
this.year = year;
this.author = author;
setStatus(status);
}