Java的重载toString()方法时类的数组给出
问题描述:
这是你如何重载的toString()
方法:
public class Person extends Object {
@Override
public final String toString() {
Gson gson = new GsonBuilder().serializeNulls().create();
return (gson.toJson(this));
}
}
在这个例子中,我调用的toString()时
人员的方法获得一个JSON字符串
,而不是默认重新串一个对象的presentation
。
In this example I get a JSON string when calling the toString()
method of Person
instead of the default string representation of an Object
.
但如果我有人员组成的数组
这样的:
Person[] persons = new Person[3];
System.out.println(persons.toString());
什么我必须做或方法(S)我要在这种情况下,覆盖?
What do I have to do or which method(s) do I have to override in that case?
答
您不能覆盖默认的磁盘阵列的的toString
。如果你要的对象数组转换成字符串,可以使用 Arrays.toString()
。
You cannot override default array's toString
. If you want to convert an array of objects into the string, use Arrays.toString()
.