Generate, Rename and Delete Getters/Setters Instantly in ...

文章推薦指數: 80 %
投票人數:10人

generate getters and setters · create the fields you want in the class then press alt+shift+s, r . a dialog will pop up allowing you to choose ... Like (1) Comment Save Tweet 151.32K Views JointheDZonecommunityandgetthefullmemberexperience. JoinForFree despitetheargumentsanddebatesaboutgettersandsettersinjava, thefactisthatthey’rearealityandyouhavetoworkwiththem. butmanaginggettersandsettersisatime-consumingeffort.creating agetter/setterfor5fieldsinaclasscantakeminutes,renamingone iserror-proneanddeletingoneisjustplaininconvenient. thereareoptionslike projectlombok (thatimplicitlycreatesgetters/setterswithouttheneedtocodethem) andyoucouldavoidgetters/settersaltogetherbyredesigningyour classes. buttheseoptionsaren’talwaysavailable,soit’sagoodthing eclipsehassomehandyfeaturesformanaginggettersandsetters. combinedwiththe abilitytogenerateconstructorsbasedonfields ,youcangettheboilerplatecodeoutofthewayinsecondsandgetonwiththerealcoding. generategettersandsetters togenerategettersandsetters,dothefollowing: createthefieldsyouwantintheclassthenpress alt+shift+s,r .adialogwillpopupallowingyoutochoosethefieldsyouwanttogenerategettersandsettersfor. click selectall tocreategetters/settersforallfields.ofcourseyoucanchooseindividualfieldsasrequired. change insertionpoint to lastmember . thistellseclipsethatyouwanttoputthemethodsatthebottomofthe class.thisisnormallythebestoptionformeasiwantthemoutof theway. click ok .eclipsewillcreatethegettersandsettersforyou. here’sanexampleofwhatthedialogshouldlooklike. note: bydefault,eclipsedoesn’tallowyoutocreateasetterfora final field–thesetterjustdoesn’tappearinthedialog.thiscanbea nuisance,especiallyifyou’veenabledautoformattingtomakefields final wherepossible.tobypassthisrestriction,enablethecheckbox allowsettersforfinalfields onthedialog.thesetterforthefieldwillnowappearinthedialog.onceyouclickok,eclipsewillremovethe final keywordfromthefieldandgeneratethesetter.eclipsealsoremembersthissetting. anotherwaytoaddjustasinglegetter/setteristo positionyourcursoranywhereintheclass(outsideanymethod),start typingeither“get”or“set”andpress ctrl+space .the optionsontheautocompletemenuwillincludeanygetters/settersof fieldsthatdon’thaveanydefinedyet.thisisaquickwaytocreatea singlegetter/setterbutisn’tgearedforbulkcreation. here’sanexampleofhowtheautocompletelooks: renamegettersandsetters theeasiestwaytorenamegetters/settersistousetherenamerefactoring. placeyourcursoronthefieldname(anywhereintheclass,notjustthedeclaration)andpress alt+shift+r . ifyou’reusingin-placerename(thedefault),thenjustrenamethe field,pressenterandeclipsewillrenamethecorrespondinggettersand settersaswell. ifyou’vechosentousetheclassicrefactordialog(seenotebelow)thenmakesureyouenable renamegetter and renamesetter ontherenamedialog. note: youcanchoosetodorenamingusingthetraditionalrenamedialogbygoingto window>preferences>java andunchecking renameineditorwithoutdialog . ipreferusingtherenamedialogasithighlightsthewholenameby defaultmakingiteasiertooverwriteandihavetheoptionofnot renamingthegettersandsettersifidon’twantto.theeclipsedefault thesedaysistousethenewin-placerenaming. althougheclipsewillrenamethegetter/setter,itwon’trenamethe argumentpassedtothesettermethod.ifyouwantconsistency,youcan navigatetothatmethod(eg.using ctrl+o )andrenametheargumentyourself. deletegettersandsetters deletinggettersandsettersisn’tasstraightforwardasjust deletingthefieldintheeditor.however,youcandeleteafieldand itsgetters/settersfromtheoutlineview. opentheoutlineview ( alt+shift+q,o ),selectthefieldyouwanttodeleteandpress delete (orright-click,delete).eclipsewillaskyouwhetheryouwanttodeletethegetters/settersaswell.justchoose yestoall andtheywillberemoved. youneedtohavefieldsvisibleintheoutlineviewtousethisfeature(ie.untogglethehidefieldsbutton ). youcanselectmultiplefieldssimultaneously.andyoucandelete individualgetters/setters(excludingthefield)byjustselectingthe getter/setterandpressing delete . from http://eclipseone.wordpress.com/2010/10/26/generate-rename-and-delete-getterssetters-in-eclipse/ Topics: OpinionsexpressedbyDZonecontributorsaretheirown. PopularonDZone UnderstandingBoundedContext WhatIsJaro-WinklerSimilarity? WhatD'HackIsDPoP? DebuggingJavaEqualsandHashcodePerformanceinProduction Comments JavaPartnerResources



請為這篇文章評分?