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

时间:2016-02-17 20:44:50

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

根据我的理解,toString()方法允许转换类型example: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());
    }
}

7 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:5)

如果我很清楚你的观点,你想在你的对象中添加一个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(吉他);)并将其添加到您的对象,如上所述将覆盖它。

答案 2 :(得分:4)

每个班级都有一个toString()方法。你只需要覆盖它。

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

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

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

答案 3 :(得分:3)

有很多方法可以做到。但是如果你刚刚开始编程,我会提出一个简单的方法:

将此添加到您的代码中。

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

答案 4 :(得分:3)

您只需添加一个功能

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

到你的班级。那就是它。

答案 5 :(得分:3)

制作新课程:

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

再次感谢所有人!