欢迎来到天天文库
浏览记录
ID:43708555
大小:221.50 KB
页数:48页
时间:2019-10-13
《Using Classes and Obje》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、3UsingClassesandObjects5THEDITIONLewis&LoftusjavaSoftwareSolutionsFoundationsofProgramDesign©2007PearsonAddison-Wesley.AllrightsreservedUsingClassesandObjectsWecancreatemoreinterestingprogramsusingpredefinedclassesandrelatedobjectsChapter3focuseson:objectcreationandobje
2、ctreferencestheStringclassanditsmethodstheJavastandardclasslibrarytheRandomandMathclassesformattingoutputenumeratedtypeswrapperclassesgraphicalcomponentsandcontainerslabelsandimages2©2007PearsonAddison-Wesley.AllrightsreservedOutlineCreatingObjectsTheStringClassPackages
3、FormattingOutputEnumeratedTypesWrapperClassesComponentsandContainersImages3©2007PearsonAddison-Wesley.AllrightsreservedCreatingObjectsAvariableholdseitheraprimitivetypeorareferencetoanobjectAclassnamecanbeusedasatypetodeclareanobjectreferencevariableStringtitle;Noobject
4、iscreatedwiththisdeclarationAnobjectreferencevariableholdstheaddressofanobjectTheobjectitselfmustbecreatedseparately4©2007PearsonAddison-Wesley.AllrightsreservedCreatingObjectsGenerally,weusethenewoperatortocreateanobjecttitle=newString("JavaSoftwareSolutions");Thiscall
5、stheStringconstructor,whichisaspecialmethodthatsetsuptheobjectCreatinganobjectiscalledinstantiationAnobjectisaninstanceofaparticularclass5©2007PearsonAddison-Wesley.AllrightsreservedInvokingMethodsWe'veseenthatonceanobjecthasbeeninstantiated,wecanusethedotoperatortoinvo
6、keitsmethodscount=title.length()Amethodmayreturnavalue,whichcanbeusedinanassignmentorexpressionAmethodinvocationcanbethoughtofasaskinganobjecttoperformaservice6©2007PearsonAddison-Wesley.AllrightsreservedReferencesNotethataprimitivevariablecontainsthevalueitself,butanob
7、jectvariablecontainstheaddressoftheobjectAnobjectreferencecanbethoughtofasapointertothelocationoftheobjectRatherthandealingwitharbitraryaddresses,weoftendepictareferencegraphically"SteveJobs"name1num1387AssignmentRevisitedTheactofassignmenttakesacopyofavalueandstoresiti
8、navariableForprimitivetypes:num138num296Before:num2=num1;num138num238After:8ReferenceAssignmentForobjectrefere
此文档下载收益归作者所有