//我一直在试图理解字符串是如何工作的以及它的主要功能是什么,但是我遇到了麻烦。就像下面的例子。 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