Java – what is the purpose of using the toString () method in the following code?

See the English answer > how to override tostring() properly in Java? 11

This is my code:

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

Solution

Change public string output() to public string tostring() You've built it, just give it another name

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>