我正在基于给定的GUI实现一个类,并且我试图重写tostring()以便在GUI上显示信息,但我无法让它正常工作。
GUI代码
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);
}
}对于我正在实现的类,我编写了以下方法
public String tostring(){
return currentApplicant;
}发布于 2013-04-15 14:43:14
你需要使用toString,而不是tostring,因为Java是区分大小写的。因此:
public String toString()tostring在技术上与Java语言中的toString不同。注意拼写。如果您的编辑器支持它,请使用@Override注释。@Override注解可以防止您无法覆盖正确的方法。
https://stackoverflow.com/questions/16009135
复制相似问题