布尔setter错误输出

时间:2015-03-24 14:03:15

标签: java methods boolean output setter

当我在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");
    }
}

1 个答案:

答案 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);
}