【问题标题】:How can i set the result of a method in a toString如何在 toString 中设置方法的结果
【发布时间】:2016-12-21 13:03:55
【问题描述】:

我想在我的 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 - 我真的是新手,完全是初学者。** 谢谢。

【问题讨论】:

  • 您的代码将无法编译。方法updatedPrice(double price)中没有return语句
  • 1) 使用 "return finalPrice;"在您的 updatePrice 方法中。在 toString() 调用 updatePrice 方法 2) 将 updatePrice 更改为 void 并使 finalPrice 成为实例变量而不是方法本地

标签: java oop methods tostring


【解决方案1】:

在那里调用你的方法:

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()

【讨论】:

  • +1 建议不要调用 toString() 中的方法并引用对象的状态 - 正是我正要回答的 :-)
【解决方案2】:

创建一个属性 finalPrice 并将值分配给

this.finalPrice = /*the price*/

你的代码可以工作

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 2011-04-06
      相关资源
      最近更新 更多