Java toString方法重寫工具之ToStringBuilder案例詳解
apache的commons-lang3的工具包里有一個(gè)ToStringBuilder類,這樣在打日志的時(shí)候可以方便的打印出類實(shí)例中的各屬性的值。
具體用法如下:
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
]
默認(rèn)一行的:
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
原理其實(shí)就是通過JAVA的reflect(反射)獲取值,然后組成一個(gè)Buffer。
里面部分源碼:
/** * <p>Append to the <code>toString</code> the start of data indicator.</p> * 拼裝結(jié)果的 * @param buffer the <code>StringBuffer</code> to populate * @param object the <code>Object</code> to build a <code>toString</code> 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); } } } /** * <p>Append the {@link System#identityHashCode(java.lang.Object)}.</p> * 拼裝對(duì)象hashcode * @param buffer the <code>StringBuffer</code> to populate * @param object the <code>Object</code> 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修飾的屬性不能打印出來,但是父類的是可以打印出來的,使用的時(shí)候一定要注意了。
到此這篇關(guān)于Java toString方法重寫工具之ToStringBuilder案例詳解的文章就介紹到這了,更多相關(guān)Java toString方法重寫工具之ToStringBuilder內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java switch 語句如何使用 String 參數(shù)
這篇文章主要介紹了Java switch 語句如何使用 String 參數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06JDBC連接SQL?Server數(shù)據(jù)庫實(shí)現(xiàn)增刪改查的全過程
實(shí)際開發(fā)中手動(dòng)的輸入SQL語句是少之又少,大多數(shù)情況下是通過編譯代碼進(jìn)行來控制自動(dòng)執(zhí)行,下面這篇文章主要給大家介紹了關(guān)于JDBC連接SQL?Server數(shù)據(jù)庫實(shí)現(xiàn)增刪改查的相關(guān)資料,需要的朋友可以參考下2023-04-04SpringBoot 對(duì)象存儲(chǔ) MinIO的詳細(xì)過程
MinIO 是一個(gè)基于 Go 實(shí)現(xiàn)的高性能、兼容 S3 協(xié)議的對(duì)象存儲(chǔ),它適合存儲(chǔ)海量的非結(jié)構(gòu)化的數(shù)據(jù),這篇文章主要介紹了SpringBoot 對(duì)象存儲(chǔ) MinIO,需要的朋友可以參考下2023-07-07如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化
這篇文章主要介紹了如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07javaBean的基礎(chǔ)知識(shí)及常見亂碼解決方法
這篇文章主要介紹了javaBean的基礎(chǔ)知識(shí)及常見亂碼解決方法的相關(guān)資料,需要的朋友可以參考下2017-03-03