首页 > 编程知识 正文

tostring和valueof(实体类中的tostring)

时间:2023-05-04 08:16:49 阅读:76203 作者:3105

群集语言构建器(公共语言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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。