我如何在tostring中设置方法的结果



我想在我的tostring中显示一种方法的结果,我该怎么做?到目前为止,这是我的代码:您可以在底线上看到我不知道要为获得" UpdatedPrice"的结果写的内容,您可以帮忙吗?

        public double updatedPrice(double price){
        this.price=price;
        double ChangePriceRate, ChangePriceAmount, finalPrice;
        if(name=="Bamba"){
            ChangePriceRate = 0.15;
        }else{
            ChangePriceRate = 0.05;
        }
        ChangePriceAmount = price * ChangePriceRate;
        if(name=="Bamba"){
            finalPrice = price + ChangePriceAmount;
        }else{
            finalPrice = price - ChangePriceAmount;
    }


    }
    public String toString (){
        return  "Name of the Snack: "+name+ "n"+
                "Name of the Company: "+comp+ "n"+
                "Price before discount: "+this.price+ "n"+
                "Price after discount: "+        **finalPrice?**      + "n";
    }

b.t.w - 我真的是新手,一个总的乞eg。**谢谢。

只需在那里调用您的方法:

public String toString (){
    return  "Name of the Snack: " + name + "n" +
            "Name of the Company: " + comp + "n" +
            "Price before discount: " + this.price+ "n" +
            "Price after discount: " + updatedPrice(this.price) + "n";
}

注意:
通常,我建议不要在toString()方法中调用方法。如果您仅显示toString()内的类状态,因此只会显示现有字段的值。

,那会更好。

这意味着您应该引入一个称为finalPrice的字段并将其值存储在此处。之后,您可以使用toString()方法显示此值:

public static class MyClass {
    private String name;
    private String comp;
    private double price;
    private double finalPrice; // <-- Field for final price
    [...]    
    public void updatePrice(double price) {
      this.price = price;
      double changePriceRate;
      double changePriceAmount;
      if ("Bamba".equals(this.name)) { // <-- Use equals()!
        changePriceRate = 0.15;
      } else {
        changePriceRate = 0.05;
      }
      changePriceAmount = price * changePriceRate;
      if ("Bamba".equals(this.name)) { // <-- Use equals()!
        finalPrice = price + changePriceAmount;
      } else {
        finalPrice = price - changePriceAmount;
      }
    }
    public String toString() {
      return "Name of the Snack: " + name + "n" +
             "Name of the Company: " + comp + "n" +
             "Price before discount: " + price + "n" +
             "Price after discount: " + finalPrice + "n";
    }
  }

奖励点:
请勿使用==进行比较字符串,如果要比较字符串的内容!

,请使用equals()

创建属性Finalprice并将值分配给

this.finalPrice = /*the price*/

您的代码将起作用

将FinalPrice变量存储为实例变量:

double finalPrice;
    public double updatedPrice(double price){
        this.price=price;
        double ChangePriceRate, ChangePriceAmount;
        if(name=="Bamba"){
            ChangePriceRate = 0.15;
        }else{
            ChangePriceRate = 0.05;
        }
        ChangePriceAmount = price * ChangePriceRate;
        if(name=="Bamba"){
            finalPrice = price + ChangePriceAmount;
        }else{
            finalPrice = price - ChangePriceAmount;
        }
        return finalPrice;
    }
    public String toString (){
        return  "Name of the Snack: "+name+ "n"+
                "Name of the Company: "+comp+ "n"+
                "Price before discount: "+this.price+ "n"+
                "Price after discount: "+        finalPrice      + "n";
    }

和另一个提示:名称变量总是在开始时带有小写字母,它可以帮助您区分班级名称和变量名称。

最新更新