Java的重载toString()方法时类的数组给出 [英] Java overloading toString() method when array of class is given
本文介绍了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()
.
这篇关于Java的重载toString()方法时类的数组给出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文