//我一直在試圖理解字符串是如何工作的以及它的主要功能是什麼,但是我遇到了麻煩。就像下面的例子。 toString方法到底做了什麼?toString方法如何工作?
public class Item {
private String name;
private int price;
private int qty;
public Item() {
}
public Item(String n, int p, int q){
this.name=n;
this.price=p;
this.qty=q;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getQty() {
return qty;
}
public String toString() {
return name +": $" + price+":" + qty;
}
有意義...... – duffymo 2013-03-21 15:18:33
要添加到此:應該說'toString'可以用來完全複製對象。例如,'Tile(int x,int y,int z)'會打印出來,如:'Tile [x = 5] [y = 1] [z = 7]'。 – 2013-03-21 15:20:16