如何在 toString 中设置方法的结果

How can i set the result of a method in a 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 - 我对此很陌生,完全是初学者。** 谢谢。

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

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";
}

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


这意味着您应该引入一个名为 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";
    }

还有一个提示:命名变量总是以小写字母开头,它可以帮助您区分 class 名称和变量名称。