在Java类中编写toString方法时,是否应该在toString中使用该字段,还是应该使用该字段的类getter?即:
private String name;
public NameClass(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return "This is the name:" + this.name;
}
// OR THIS
public String toString() {
return "This is the name: " + getName();
}