Generate, Rename and Delete Getters/Setters Instantly in ...
文章推薦指數: 80 %
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
延伸文章資訊
- 1Generate, Rename and Delete Getters/Setters Instantly in ...
generate getters and setters · create the fields you want in the class then press alt+shift+s, r ...
- 2Eclipse 使用技巧-自動產生Getters 與Setters - 搞笑談軟工
在Eclipse中,可以在該程式的工作區域中,按下滑鼠右鍵,選擇SourceàGenerate Getters and Setters以自動產生Getters 與 Setters(參考圖2)。
- 3Say “bye bye!!!” to Annoying Getters/Setters & Shorten your ...
When you annotate your Entity class with @Getter and @Setter , it will generate getter methods an...
- 4How do I generate getters and setters? - avajava.com
To generate getters and setters, I right-click the editor and go to Source → Generate Getters and...
- 5Is there a way to automatically generate getters and setters in ...
1) Press Alt + Shift + S , then R · 2) Right click -> Source -> Generate Getters & Setters · 3) G...