将ExamQuestion @ 143c8b3作为打印输出在控制台中 [英] Getting ExamQuestion@143c8b3 as print out in console
问题描述
我有一个类有一个方法,它接受一个字符串。然后我有一个方法返回上述方法的值。当我得到我的主类中的返回值并打印它的打印值是ExamQuestion @ 143c8b3。如何让它正确打印?
I have a have a class that has a method which takes a string. Then I have a method which returns the value of the above method. When ever I get the return value in my main class and print it the printed value is "ExamQuestion@143c8b3". How do I get it to print correctly?
感谢
推荐答案
是对象的toString()方法的默认格式。
Whenever you get the format "@" this is the default format of an object's toString() method.
调用 System.out.println(question);
调用 ExamQuestion.toString
。如果方法没有被覆盖,超级类中的版本将被调用,在这种情况下它将是 Object
的版本。
Calling System.out.println(question);
calls ExamQuestion.toString()
. If the method isn't overridden, the version in the super class will be called, in this case it will be Object
's version.
这就是为什么你得到ExamQuestion @ 143c8b3。
So that's why you get ExamQuestion@143c8b3.
要解决此问题,请在您的ExamQuestion类中添加以下方法:
To fix this, put the following method in your ExamQuestion class:
public String toString() {
// return a string that means something here
}
这篇关于将ExamQuestion @ 143c8b3作为打印输出在控制台中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!