简体   繁体   English

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

[英]How can i set the result of a method in a toString

I want to display the result of a method in my ToString, how can I do that? 我想在ToString中显示方法的结果,该怎么办? here is my code so far: you can see at the bottom line that I don't know what to write for getting the result of the "updatedPrice", can u help? 到目前为止,这是我的代码:您可以在底行看到不知道写什么才能获取“ 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";
    }

btw - I'm really new to this, a total begginer.** thank you. 顺便说一句 -我真的很陌生,完全是个开始。**谢谢。

Just call your method there: 只需在此处调用您的方法:

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

Attention: 注意:
Generally I would advise AGAINST calling methods in the toString() method. 通常,我建议在toString()方法中使用AGAINST调用方法。 It would be better if you only show the state of the class inside toString() and therefore only show the values of existing fields. 如果只在toString()显示类的状态,因此仅显示现有字段的值,那会更好。


This means in consequence that you should introduce a field called finalPrice and store your value there. 因此,这意味着您应该引入一个名为finalPrice的字段并在其中存储您的值。 After that you can show this value using the toString() method: 之后,您可以使用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";
    }
  }

Bonus point: 奖励积分:
Do not use == for comparing strings, use equals() instead if you want to compare the contents of strings! 如果要比较字符串的内容,请不要使用==来比较字符串,而应使用equals()

create a property finalPrice and assign the value to 创建属性finalPrice并将值分配给

this.finalPrice = /*the price*/

and your code will work 并且您的代码将正常工作

store the finalPrice variable as an instance variable: 将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";
    }

and another hint: name variables always with a lowercase letter at the beginning, it helps you to differentiate between class names and variable names. 另一个提示:名称变量始终以小写字母开头,这有助于您区分类名和变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM