首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重写tostring()

重写tostring()
EN

Stack Overflow用户
提问于 2013-04-15 14:41:22
回答 1查看 267关注 0票数 1

我正在基于给定的GUI实现一个类,并且我试图重写tostring()以便在GUI上显示信息,但我无法让它正常工作。

GUI代码

代码语言:javascript
复制
private void updateDisplay() {
    try {
        if (game.isAccepted()) {
            String str = "Selected Applicant:\n\n"
                    + currentApplicant.toString() + "\n\nwas ";
            if (game.isBestApplicant()) {
                str += "the BEST.  You Win!";
            } else {
                str += "not the best available.  You Lose.\n\nBest applicant was: \n\n"
                        + game.getBestApplicant();
            }
            display.setText(str);
            showNewGameControls();
        } else {
            display.setText("Current applicant is:\n\n"
                    + currentApplicant.toString()
                    + "\n\nDo you wish to accept or reject?");
            showCurrentGameControls();
        }
    } catch (HiringException e) {
        display.setText(e.getMessage());
    } catch (Exception e) {
        display.setText("Unandled Exception: " + e.toString());
        throw new RuntimeException(e);
    }
}

对于我正在实现的类,我编写了以下方法

代码语言:javascript
复制
public String tostring(){
  return currentApplicant;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-15 14:43:14

你需要使用toString,而不是tostring,因为Java是区分大小写的。因此:

代码语言:javascript
复制
public String toString()

tostring在技术上与Java语言中的toString不同。注意拼写。如果您的编辑器支持它,请使用@Override注释。@Override注解可以防止您无法覆盖正确的方法。

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

https://stackoverflow.com/questions/16009135

复制
相关文章

相似问题

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