这 toString()
方法返回对象的字符串表示形式。它广泛用于调试,在日志中打印对象的内容等。这篇文章将讨论如何覆盖 toString()
Java中的方法。
对象类已经包含 toString()
方法,它返回一个“以文本方式表示”对象的字符串。的默认实现 Object.toString()
方法返回一个由类名组成的字符串, '@'
字符,后跟对象哈希码的无符号十六进制表示。 IE,
1 2 3 |
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } |
由于所有 Java 对象都继承自 java.lang.Object
,您需要覆盖 toString()
方法来获得所需的字符串表示。否则,上述默认实现 toString()
当您尝试打印对象时将调用方法。例如,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Country { private String name; private String continent; private long population; public Country(String name, String continent, long population) { this.name = name; this.continent = continent; this.population = population; } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 331449281); System.out.println(country); } } |
输出:
Country@5197848c
要打印对象的内容,您必须让您的类覆盖 toString()
方法。这可以通过多种方式完成:
1. 使用字符串连接运算符
您可以使用字符串连接运算符 +
清晰简洁地在类中附加成员变量。虽然字符串连接使用 +
运算符在 Java 中不推荐使用,但可以在没有循环的情况下完成,因为 Java 编译器隐式创建了一个中间 StringBuilder
目的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Country { private String name; private String continent; private long population; public Country(String name, String continent, long population) { this.name = name; this.continent = continent; this.population = population; } @Override public String toString() { return "Country{" + "name='" + name + '\'' + ", continent='" + continent + '\'' + ", population=" + population + '}'; } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 331449281); System.out.println(country); } } |
输出:
Country{name='United States', continent='North America', population=331449281}
2.使用 'StringJoiner'
在 Java 8 及更高版本中,我们可以使用 StringJoiner
类,它接受一个分隔符并构造一个由分隔符分隔的值序列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.StringJoiner; class Country { private String name; private String continent; private long population; public Country(String name, String continent, long population) { this.name = name; this.continent = continent; this.population = population; } @Override public String toString() { return new StringJoiner(", ", Country.class.getSimpleName() + "[", "]") .add("name='" + name + "'") .add("continent='" + continent + "'") .add("population=" + population) .toString(); } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 331449281); System.out.println(country); } } |
输出:
Country[name='United States', continent='North America', population=331449281]
3.使用Guava的 'MoreObjects.ToStringHelper()'
Guava’s MoreObjects 类提供了几个对任何 Java 对象进行操作的辅助函数。实施 Object.toString()
方法,你可以创建一个实例 MoreObjects.ToStringHelper
, 并调用 add(name, value)
每个字段的方法。这 add(name, value)
方法以 name=value 格式将名称-值对添加到输出中。
如果您只需要在输出中添加一个未命名的值,请调用 addValue(value)
方法。此外,您可以使用 omitNullValues()
方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import com.google.common.base.MoreObjects; class Country { private String name; private String continent; private long population; public Country(String name, String continent, long population) { this.name = name; this.continent = continent; this.population = population; } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("name", name) .add("continent", continent) .add("population", population) .toString(); } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 331449281); System.out.println(country); } } |
输出:
Country{name=United States, continent=North America, population=331449281}
4. 使用 Apache Commons 'ToStringBuilder'
class
类似于 Guava MoreObjects
类,Apache Commons Lang 库提供 ToStringBuilder 协助实施的课程 Object.toString()
方法。要使用这个类,编写代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import org.apache.commons.lang3.builder.ToStringBuilder; class Country { private String name; private String continent; private long population; public Country(String name, String continent, long population) { this.name = name; this.continent = continent; this.population = population; } @Override public String toString() { return new ToStringBuilder(this) .append("name", name) .append("continent", continent) .append("population", population) .toString(); } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 331449281); System.out.println(country); } } |
输出:
Country@69d0a921[name=United States,continent=North America,population=331449281]
或者,您可以直接调用 reflectionToString()
使用反射为指定对象生成 toString 的方法。请注意,此方法在安全管理器下可能会失败,因为它使用 AccessibleObject.setAccessible
更改类中私有字段的可见性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import org.apache.commons.lang3.builder.ToStringBuilder; class Country { private String name; private String continent; private long population; public Country(String name, String continent, long population) { this.name = name; this.continent = continent; this.population = population; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 331449281); System.out.println(country.toString()); } } |
输出:
Country@67424e82[continent=North America,name=United States,population=331449281]
请注意,几乎所有 IDE(Eclipse、IntelliJ IDEA 等)都支持自动生成 toString()
方法,基于类中的成员变量。建议使用各自的 IDE 功能来生成 toString()
方法而不是自己写。例如,在 IntelliJ IDEA 中,您只需右键单击源代码,选择 Generate 菜单,然后选择 toString 选项。或者,只需按 Alt+Insert 并选择 toString 选项。
IntelliJ IDEA 进一步提供了在不同 toString 模板之间进行选择的灵活性,例如 StringBuilder、ToStringBuilder (Apache commons-lang 3)、Objects.toStringHelper (Guava)、MoreObjects.toStringHelper (Guava 18+)、StringJoiner (JDK 1.8)、String concat (+ ), ETC。
这就是关于覆盖 toString()
Java中的方法。