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

问题描述 投票:0回答:7

根据我的理解,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());
    }
}
java constructor static-methods tostring getter-setter
7个回答
10
投票

public String output()
更改为
public String toString()
。您已经构建了它,只是给了它另一个名称。


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(guitar);),并将其添加到您的对象中,如上所述将覆盖它。


4
投票

每个类都已经有一个

toString()
方法。您只需要覆盖它即可。

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

或者您可以将

output()
重命名为
toString()

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


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

3
投票

您只需添加一个功能即可

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

到你的班级。就是这样。


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 brand is: "+this.guitarMake + " and ,..";
    }

}

2
投票

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

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

再次感谢大家!

© www.soinside.com 2019 - 2024. All rights reserved.