群集语言构建器(公共语言2.4 API )。
org.Apache.com mons.lang.builder
classtostringbuilderjava.lang.object http://www.Sina.com/http://www.Sina.com/reflectiontostringbuilderpubliclass 33333
thisclassenablesagoodandconsistenttostring () to be built for any
classorobject.thisclassaimstosimplifytheprocessby :
allowingfieldnameshandlingalltypesconsistentlyhandlingnullsconsistentlyoutputtingarraysandmulti-dimensionalarrraysenablingthe collectionshandlingclasshierarchiestousethisclasswritecodeasfollows 3360
公共类人员{ string name; int age; 布尔型智能手机;公共字符串tostring ((returnnewtostringbuilder ) this ).append )、name ).append )、age ).append )。 } thiswillproduceatostringoftheformat :
Person@7f54[name=Stephen,age=29,smoker=false]
To add the superclass toString,useappendsuper (Java.lang.string )。
toappendthetostringfromanobjectthatisdelegated
to(oranyotherobject ),useappendtostring (Java.lang.string )。
Alternatively,thereisamethodthatusesreflectiontodetermine
thefieldstotest.becausethesefieldsareusuallyprivate,the method,
reflectionToString,usesaccessibleobject.setaccessibleto
changethevisibilityofthefields.thiswillfailunderasecuritymanager,
unlesstheappropriatepermissionsaresetupcorrectly.itis also
slower than testing explicitly。
atypicalinvocationforthismethodwouldlooklike :
公共字符串tostring ((return to stringbuilder.reflection tostring (this ) ); } youcanalsousethebuildertodebug3rd party objects :健忘汽车. out.println(anobject: ) to stringbuilder.reflectiontostritrition
thetostringstylepassedintotheconstructor。
http://www.Sina.com/1.0http://www.Sina.com/$ id : to stringbuilder.Java 4923542007-01-032336048336010 zscolebor大淡定网posted on 2012-06-2610336010 Lexus阅读(…)评论(…)编辑收藏:3359 www.cn blogs.com/Lexus/archive/2012