首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在下面的代码中使用toString()方法的目的是什么?

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

Stack Overflow用户
提问于 2016-02-17 20:44:49
回答 7查看 793关注 0票数 3

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

这是我的代码:

代码语言:javascript
复制
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());
    }
}
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2016-02-17 20:48:39

public String output()更改为public String toString()。你已经建好了,给它取了个名字。

票数 10
EN

Stack Overflow用户

发布于 2016-02-17 20:52:12

如果我已经很好地理解了您的观点,那么您希望向对象中添加一个toString()方法。

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

代码语言:javascript
复制
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?";

在你的主旋律中:

代码语言:javascript
复制
System.out.println(guitar); // here toString() method is called to print your guitar object's description.

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

票数 5
EN

Stack Overflow用户

发布于 2016-02-17 21:00:47

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

代码语言:javascript
复制
@Override
public String toString(){
   return output();
}

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35467349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档