Java|Java toString方法重写工具之ToStringBuilder案例详解
apache的commons-lang3的工具包里有一个ToStringBuilder类,这样在打日志的时候可以方便的打印出类实例中的各属性的值。
具体用法如下:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class Message { private String from; private String to; private String body; public String getFrom() {return from; } public void setFrom(String from) {this.from = from; } public String getTo() {return to; } public void setTo(String to) {this.to = to; } public String getBody() {return body; } public void setBody(String body) {this.body = body; } @Override public String toString() {return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } public static void main(String[] args) {Message msg = new Message(); msg.setFrom("vince"); msg.setTo("mike"); msg.setBody("hello"); System.out.println(msg.toString()); }}
而且支持多种打印格式
多行输出的:
com.vince.im.dto.Message@af72d8[
from=vince
to=mike
body=hello
]
默认一行的:
com.vince.im.dto.Message@af72d8[from=vince,to=mike,body=hello]
NO_FIELD_NAMES_STYLE:
com.vince.im.dto.Message@af72d8[vince,mike,hello]
SHORT_PREFIX_STYLE:
Message[from=vince,to=mike,body=hello]
SIMPLE_STYLE:
vince,mike,hello
原理其实就是通过JAVA的reflect(反射)获取值,然后组成一个Buffer。
里面部分源码:
/*** Append to thetoString
the start of data indicator.
* 拼装结果的* @param buffertheStringBuffer
to populate* @param objecttheObject
to build atoString
for*/public void appendStart(final StringBuffer buffer, final Object object) {if (object != null) {appendClassName(buffer, object); appendIdentityHashCode(buffer, object); appendContentStart(buffer); if (fieldSeparatorAtStart) {appendFieldSeparator(buffer); }}} /*** Append the {@link System#identityHashCode(java.lang.Object)}.
* 拼装对象hashcode* @param buffertheStringBuffer
to populate* @param objecttheObject
whose id to output*/protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) {if (this.isUseIdentityHashCode() && object!=null) {register(object); buffer.append('@'); buffer.append(Integer.toHexString(System.identityHashCode(object))); }}
需要注意的是:
Builds a toString value using the default ToStringStyle through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.
也就是说transient和static修饰的属性不能打印出来,但是父类的是可以打印出来的,使用的时候一定要注意了。
【Java|Java toString方法重写工具之ToStringBuilder案例详解】到此这篇关于Java toString方法重写工具之ToStringBuilder案例详解的文章就介绍到这了,更多相关Java toString方法重写工具之ToStringBuilder内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 对抗抑郁最好的方法
- 事件代理
- 怎样用黑谜速冻膜去黑头,|怎样用黑谜速冻膜去黑头, 最有效的去黑头的方法看这!
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 移动端h5调试方法
- 唱歌教学(导致嗓音损坏的几个常见的错误唱歌方法!)
- 拆书方法训练营
- 数组常用方法一