Java 需要有关isPerishable()的帮助:boolean

Java 需要有关isPerishable()的帮助:boolean,java,Java,如果GrocItem是易腐的,那么toString()将包括“易腐的”。如果GrocItem不是易腐的,那么toString()方法将不包括“易腐的” 这是我的代码,我被卡住了: public class GrocItem extends Item{ private boolean perishable; public GrocItem(String name, double price, int qty,boolean perishable) { super(name, price

如果GrocItem是易腐的,那么toString()将包括“易腐的”。如果GrocItem不是易腐的,那么toString()方法将不包括“易腐的”

这是我的代码,我被卡住了:

public class GrocItem extends Item{

private boolean perishable;

public GrocItem(String name, double price, int qty,boolean perishable) {
    super(name, price, qty);
    this.perishable = perishable;
    perishable = false;
    
}

public boolean isPerishable() {
    return perishable;
    
}

public void setPerishable(boolean perishable) {
    this.perishable = perishable;
}

public String toString() {
    return getName() + "\t" + getPrice() + "\t" + getQty();
 }

}

可能在toString中使用if语句:

public String toString() {
    if(isPerishable()){
        return getName() + "\t" + getPrice() + "\t" + getQty() + "\t" + "Perishable";
    } else {
        return getName() + "\t" + getPrice() + "\t" + getQty();
    }
 }

你的尝试在哪里?如果,您知道
吗?这是正确的。然而,你只是在没有他/她自己努力的情况下为他/她做了OP的家庭作业。