第1行顯示的內容如下? BlueJ打印A @ 18fea98,但我不認爲這是正確的。請幫忙,謝謝。什麼是輸出?
class A{
private int x;
public A(){
x=0;
}
}
//test code in client program
A test = new A();
out.println(test);//LINE 1
第1行顯示的內容如下? BlueJ打印A @ 18fea98,但我不認爲這是正確的。請幫忙,謝謝。什麼是輸出?
class A{
private int x;
public A(){
x=0;
}
}
//test code in client program
A test = new A();
out.println(test);//LINE 1
默認情況下,out.println(obj)
將調用對象上的測試方法toString
,默認情況下將返回其上toString
被調用對象的HashBased存儲器位置。
在您的方案:[email protected]
其中,預計輸出。如果您需要打印的X屬性的值,你有以下兩種選擇
toString
方法上的一個類來返回你想要例如:
class A{
private int x;
public A() {
x = 0;
}
public String toString() {
return "A:"+x;
}
}
謝謝,我如何重寫toString方法? –
我已經用示例實現更新了答案,在當前情況下輸出將爲「A:0」。根據需要隨意修改toString實現。如果答案滿意,請接受答案,以便其他人可能會覺得有幫助 –
你還指望它來打印? – Amit
這是正確的 - http://stackoverflow.com/questions/4712139/why-does-the-default-object-tostring-include-the-hashcode – radoh
「類Object的toString方法返回一個字符串組成的名稱對象是實例的類,符號字符「@」和對象的哈希代碼的無符號十六進制表示形式。「 – BoDidely