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

时间:2016-12-21 13:03:55

标签: java oop methods 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 - 我真的很陌生,这是一个彻头彻尾的问题。** 谢谢。

3 个答案:

答案 0 :(得分:2)

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

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()方法中的AGAINST调用方法。 如果你只在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()

答案 1 :(得分:0)

创建一个属性finalPrice并将值赋给

this.finalPrice = /*the price*/

您的代码将正常工作

答案 2 :(得分:0)

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

和另一个提示:名称变量始终以小写字母开头,它可以帮助您区分类名和变量名。