【问题标题】:what would be the purpose of using the toString() method in the code below? [duplicate]在下面的代码中使用 toString() 方法的目的是什么? [复制]
【发布时间】:2016-05-29 18:51:28
【问题描述】:

据我了解,toString() 方法允许转换类型示例:int x = 5, sys....(x.toString()) 并将 5 打印到控制台。但是与下面的代码相比,这样做的好处/坏处是什么? (不是说我的更好,我真的很好奇)

这是我的代码:

public class GitFiddle {

    //Initalize class variables.
    private String guitarMake;
    private String guitarModel;
    private int numOfStrings;
    private String notes;
    private int jumboFrets;
    private String neckType;
    private String fingerBoard;
    private String humPickUps;
    private boolean tuned;


    //A constructor that has specific variables assigned to it.
    public GitFiddle (String guitarMake, String guitarModel, int    numOfStrings,
    String notes, int jumboFrets, String neckType, String fingerBoard,
    String humPickUps, boolean tuned) {
        this.guitarMake = guitarMake;
        this.guitarModel = guitarModel;
        this.numOfStrings = numOfStrings;
        this.notes = notes;
        this.jumboFrets = jumboFrets;
        this.neckType = neckType;
        this.fingerBoard = fingerBoard;
        this.humPickUps = humPickUps;
        this.tuned = tuned;
    }

    //Created the output that will be displayed to the user. 
    public String output()
    {
        return "My guitar is an " + guitarMake + "," + " " + guitarModel + "  which is a " + 
        numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
        + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
        " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
        " humbucker pickups which is perfect for some sweet metal action!" + 
        "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +   "\nAre you ready?";
    }

    public static void main(String args[])
    {
        //Create an instance of household item method.
        GitFiddle guitar = new GitFiddle ("Ibanez", "S-7 320 EX", 7, "B E A D G B E", 22, "Wizard, W-7 II neck", "rosewood", "EMG 81 85", true);

        //Output the status of the household item.
        System.out.println(guitar.output());
    }
}

【问题讨论】:

  • 据我了解,toString() 方法将返回有关仪器的信息,所以它不正是您在 output() 方法中所做的吗?
  • 您的output() 方法本质上是在做toString() 通常做的事情。改变它的名字,你就有了一个toString() 方法。
  • 为什么还要回复一个重复的答案生成的帖子,如果你不愿意在不居高临下的情况下表现出建设性,那么为什么还要回复帖子

标签: java constructor static-methods tostring getter-setter


【解决方案1】:

在接受建议并编辑适当部分后回答:

public class GitFiddle {

    //Initalize class variables.
    private String guitarMake;
    private String guitarModel;
    private int numOfStrings;
    private String notes;
    private int jumboFrets;
    private String neckType;
    private String fingerBoard;
    private String humPickUps;
    private boolean tuned;


    //A constructor that has specific variables assigned to it.
    public GitFiddle (String guitarMake, String guitarModel, int numOfStrings, String notes, int jumboFrets, String neckType, String fingerBoard, String humPickUps, boolean tuned) {
        this.guitarMake = guitarMake;
        this.guitarModel = guitarModel;
        this.numOfStrings = numOfStrings;
        this.notes = notes;
        this.jumboFrets = jumboFrets;
        this.neckType = neckType;
        this.fingerBoard = fingerBoard;
        this.humPickUps = humPickUps;
        this.tuned = tuned;
    }

    //Created the output that will be displayed to the user. 
    public String toString()
    {
        return "My guitar is an " + guitarMake + "," + " " + guitarModel + " which is a " + 
        numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
        + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
        " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
        " humbucker pickups which is perfect for some sweet metal action!" + 
        "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +  "\nAre you ready?";
    }

    public static void main(String args[])
    {
        //Create an instance of household item method.
        GitFiddle guitar = new GitFiddle ("Ibanez", "S-7 320 EX", 7, "B E A D G B E", 22, "Wizard, W-7 II neck", "rosewood", "EMG 81 85", true);

        //Output the status of the household item.
        System.out.println(guitar.toString());
    }
}

再次感谢大家!

【讨论】:

  • 您更新我们真是太好了 :) 由于这是在您收到建议之后,您不需要发布答案。您只需要对对您有帮助的答案进行投票,并接受对您帮助最大的答案(如果有的话)。这样一来,您将奖励所有帮助过您的贡献者 :) - 顺便说一句,这个练习很有趣!
  • 哈哈@shlublu,很高兴知道,除了是 java/programming 的新手,我显然对 stackoverflow 还是陌生的,但所有答案都得到了支持,因为他们都帮助并检查了我使用的答案!再次感谢
【解决方案2】:

每个类都已经有一个toString() 方法。你只需要覆盖它。

@Override
public String toString(){
   return output();
}

或者您可以将output() 重命名为toString()

通常 toString() 方法用于获取有关类内容的信息。 我

【讨论】:

    【解决方案3】:

    如果我很好地理解了您的观点,您想为您的对象添加一个 toString() 方法。

    实际上,您的 output() 方法可以替换为 toString() 方法(当您要打印对象时会调用该方法)。

    public String toString():
        return "My guitar is an " + guitarMake + "," + " " + guitarModel + "  which is a " + 
        numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
        + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
        " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
        " humbucker pickups which is perfect for some sweet metal action!" + 
        "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +   "\nAre you ready?";
    

    在你的主要:

    System.out.println(guitar); // here toString() method is called to print your guitar object's description.
    

    此外,toString() 方法已经实现(尝试 System.out.println(guitar);),如上将其添加到您的对象将覆盖它。

    【讨论】:

      【解决方案4】:

      public String output() 更改为public String toString()。你已经建立了它,只是给它起了另一个名字。

      【讨论】:

      • 我会删除"My guitar is an " +,因为可以预期toString() 是客观的,但我完全同意。
      【解决方案5】:

      新建类:

       public class GitFiddle {
          private String guitarMake;
          private String guitarModel;
          private int numOfStrings;
          private String notes;
          private int jumboFrets;
          private String neckType;
          private String fingerBoard;
          private String humPickUps;
          private boolean tuned;
      
          //A constructor that has specific variables assigned to it.
          public GitFiddle (String guitarMake, String guitarModel, int    numOfStrings,
                            String notes, int jumboFrets, String neckType, String fingerBoard,
                            String humPickUps, boolean tuned) {
              this.guitarMake = guitarMake;
              this.guitarModel = guitarModel;
              this.numOfStrings = numOfStrings;
              this.notes = notes;
              this.jumboFrets = jumboFrets;
              this.neckType = neckType;
              this.fingerBoard = fingerBoard;
              this.humPickUps = humPickUps;
              this.tuned = tuned;
          }
      
      
          @Override
          public String toString() {
              return "my guitar Marke is: "+this.guitarMake + " and ,..";
          }
      
      }
      

      【讨论】:

        【解决方案6】:

        你只需添加一个函数

        public String toString(){
         String text = "Your text.";
         return text;
        }
        

        到你的班级。就是这样。

        【讨论】:

        • 所以我喜欢所有的建议,但是为了保持简单并得到我认为分配的要求,我所做的只是改变:Public String ouput() to Public String toString( ) 并制作 System.out.println 以反映更改,即:guitar.toString());感谢您的所有反馈,希望我在作业中继续保持 100% 的成绩!
        【解决方案7】:

        有很多奇妙的方法可以做到这一点。但如果你刚刚开始编程,我推荐一种简单的方法:

        将此添加到您的代码中。

        @Override
        public String toString() {
            return "My guitar is an " + guitarMake + "," + " " + guitarModel + "  which is a " + 
            numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
            + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
            " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
            " humbucker pickups which is perfect for some sweet metal action!" + 
            "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +   "\nAre you ready?";
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-27
          • 2018-05-26
          • 2017-06-03
          • 2017-02-26
          • 2016-05-23
          • 1970-01-01
          相关资源
          最近更新 更多