当前位置: 首页 > 知识库问答 >
问题:

在下面的代码中使用toString()方法的目的是什么?[副本]

鲜于喜
2023-03-14

据我所知,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());
    }
}

共有3个答案

党祖鹤
2023-03-14

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

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

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

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

蒲昊苍
2023-03-14

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

事实上,您的out()方法可以替换为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()方法已经实现(try System.out.println(guitar);)如上所述将其添加到对象将覆盖它。

束雅达
2023-03-14

将公共字符串输出()更改为公共字符串到字符串()。你已经建造了它,只是给它起了另一个名字。

 类似资料:
  • 如果(新名称) 我可以理解在vanillaJS中检查newName是否真实,但在Typescript中,这是为了什么?Typescript已经保证newName是string,并且它具有。长度属性。 完整代码在这里:https://www.typescriptlang.org/docs/handbook/classes.html

  • 问题内容: 我看到Google用尖括号注释了一些Java代码。 例如: 这种风格有什么特殊用途吗? 问题答案: 这些片段是Firebase文档的链接器。 您可以在Firebase站点上单击类名,然后转到示例代码的相应块。 很难说它是动态生成的HTML,但是对于纯Java中的注释没有这种特殊的语法

  • 我正在做一项任务来编译Pascal的子集,在初始程序中有一行代码如下: 这让我非常困惑,因为返回一个布尔值或,因此{Bool,Int}中的 中的

  • 本文向大家介绍java中的 toString()方法实例代码,包括了java中的 toString()方法实例代码的使用技巧和注意事项,需要的朋友参考一下 前言:       toString()方法 相信大家都用到过,一般用于以字符串的形式返回对象的相关数据。   最近项目中需要对一个ArrayList<ArrayList<Integer>> datas  形式的集合处理。   处理要求把集合数

  • 下面是我的代码: 和控制台输出以下内容: 我以为使用toString可以去掉[],为什么它还在那里? 编辑:如果toString不是摆脱[]的正确方法,那么正确的方法是什么?

  • 问题内容: 我想了解和的声明之间的区别。特别是方括号部分的目的是什么?什么时候使用它们,什么时候不使用? 问题答案: 它使AngularJS代码最小化。AngularJS使用参数名称将值注入到控制器函数中。在JavaScript压缩过程中,这些参数被重命名为较短的字符串。通过使用字符串数组告诉哪些参数被注入到函数中,当重命名参数时,AngularJS仍然可以注入正确的值。