软件工程外文文献翻译.docx
- 文档编号:27670613
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:9
- 大小:22.69KB
软件工程外文文献翻译.docx
《软件工程外文文献翻译.docx》由会员分享,可在线阅读,更多相关《软件工程外文文献翻译.docx(9页珍藏版)》请在冰豆网上搜索。
软件工程外文文献翻译
西安邮电学院
毕业设计(论文)
外文文献翻译
院系:
计算机学院
专业:
软件工程
班级:
软件0601
学生姓名:
导师姓名:
职称:
副教授
起止时间:
2010年3月8日至2010年6月11日
Classes
OneofthemostcompellingfeaturesaboutJavaiscodereuse.Buttoberevolutionary,you’vegottobeabletodoalotmorethancopycodeandchangeit.
That’stheapproachusedinprocedurallanguageslikeC,andithasn’tworkedverywell.LikeeverythinginJava,thesolutionrevolvesaroundtheclass.Youreusecodebycreatingnewclasses,butinsteadofcreatingthemfromscratch,youuseexistingclassesthatsomeonehasalreadybuiltanddebugged.Thetrickistousetheclasseswithoutsoilingtheexistingcode.
ØInitializingthebaseclass
Sincetherearenowtwoclassesinvolved—thebaseclassandthederivedclass—insteadofjustone,itcanbeabitconfusingtotrytoimaginetheresultingobjectproducedbyaderivedclass.Fromtheoutside,itlookslikethenewclasshasthesameinterfaceasthebaseclassandmaybesomeadditionalmethodsandfields.Butinheritancedoesn’tjustcopytheinterfaceofthebaseclass.Whenyoucreateanobjectofthederivedclass,itcontainswithinitasubobjectofthebaseclass.Thissubobjectisthesameasifyouhadcreatedanobjectofthebaseclassbyitself.It’sjustthatfromtheoutside,thesubobjectofthebaseclassiswrappedwithinthederived-classobject.
Ofcourse,it’sessentialthatthebase-classsubobjectbeinitializedcorrectly,andthere’sonlyonewaytoguaranteethis:
performtheinitializationintheconstructorbycallingthebase-classconstructor,whichhasalltheappropriateknowledgeandprivilegestoperformthebase-classinitialization.Javaautomaticallyinsertscallstothebase-classconstructorinthederived-classconstructor.
ØGuaranteeingpropercleanup
Javadoesn’thavetheC++conceptofadestructor,amethodthatisautomaticallycalledwhenanobjectisdestroyed.ThereasonisprobablythatinJava,thepracticeissimplytoforgetaboutobjectsratherthantodestroythem,allowingthegarbagecollectortoreclaimthememoryasnecessary.
Oftenthisisfine,buttherearetimeswhenyourclassmightperformsomeactivitiesduringitslifetimethatrequirecleanup.AsmentionedinChapter4,youcan’tknowwhenthegarbagecollectorwillbecalled,orifitwillbecalled.Soifyouwantsomethingcleanedupforaclass,youmustexplicitlywriteaspecialmethodtodoit,andmakesurethattheclientprogrammerknowsthattheymustcallthismethod.
Notethatinyourcleanupmethod,youmustalsopayattentiontothecallingorderforthebase-classandmember-objectcleanupmethodsincaseonesubobjectdependsonanother.Ingeneral,youshouldfollowthesameformthatisimposedbyaC++compileronitsdestructors:
firstperformallofthecleanupworkspecifictoyourclass,inthereverseorderofcreation.(Ingeneral,thisrequiresthatbase-classelementsstillbeviable.)Thencallthebase-classcleanupmethod,asdemonstratedhere
ØNamehiding
IfaJavabaseclasshasamethodnamethat’soverloadedseveraltimes,redefiningthatmethodnameinthederivedclasswillnothideanyofthebase-classversions(unlikeC++).Thusoverloadingworksregardlessofwhetherthemethodwasdefinedatthislevelorinabaseclass,it’sfarmorecommontooverridemethodsofthesamename,usingexactlythesamesignatureandreturntypeasinthebaseclass.Itcanbeconfusingotherwise(whichiswhyC++disallowsit—topreventyoufrommakingwhatisprobablyamistake).
ØChoosingcompositionvs.inheritance
Bothcompositionandinheritanceallowyoutoplacesubobjectsinsideyournewclass(compositionexplicitlydoesthis—withinheritanceit’simplicit).Youmightwonderaboutthedifferencebetweenthetwo,andwhentochooseoneovertheother.
Compositionisgenerallyusedwhenyouwantthefeaturesofanexistingclassinsideyournewclass,butnotitsinterface.Thatis,youembedanobjectsothatyoucanuseittoimplementfunctionalityinyournewclass,buttheuserofyournewclassseestheinterfaceyou’vedefinedforthenewclassratherthantheinterfacefromtheembeddedobject.Forthiseffect,youembedprivateobjectsofexistingclassesinsideyournewclass.
Sometimesitmakessensetoallowtheclassusertodirectlyaccessthecompositionofyournewclass;thatis,tomakethememberobjectspublic.Thememberobjectsuseimplementationhidingthemselves,sothisisasafethingtodo.Whentheuserknowsyou’reassemblingabunchofparts,itmakestheinterfaceeasiertounderstand.
Whenyouinherit,youtakeanexistingclassandmakeaspecialversionofit.Ingeneral,thismeansthatyou’retakingageneral-purposeclassandspecializingitforaparticularneed
ØThefinalkeyword
Java’sfinalkeywordhasslightlydifferentmeaningsdependingonthecontext,butingeneralitsays“Thiscannotbechanged.”Youmightwanttopreventchangesfortworeasons:
designorefficiency.Becausethesetworeasonsarequitedifferent,it’spossibletomisusethefinalkeyword
Thefollowingsectionsdiscussthethreeplaceswherefinalcanbeused:
fordata,methods,andclasses.
ØFinaldata
Manyprogramminglanguageshaveawaytotellthecompilerthatapieceofdatais“constant.”Aconstantisusefulfortworeasons:
Itcanbeacompile-timeconstantthatwon’teverchange.
Itcanbeavalueinitializedatruntimethatyoudon’twantchanged.
Inthecaseofacompile-timeconstant,thecompilerisallowedto“fold”theconstantvalueintoanycalculationsinwhichit’sused;thatis,thecalculationcanbeperformedatcompiletime,eliminatingsomerun-timeoverhead.InJava,thesesortsofconstantsmustbeprimitivesandareexpressedwiththefinalkeyword.Avaluemustbegivenatthetimeofdefinitionofsuchaconstant.
Afieldthatisbothstaticandfinalhasonlyonepieceofstoragethatcannotbechanged.
Whenusingfinalwithobjectreferencesratherthanprimitives,themeaninggetsabitconfusing.Withaprimitive,finalmakesthevalueaconstant,butwithanobjectreference,finalmakesthereferenceaconstant.Oncethereferenceisinitializedtoanobject,itcanneverbechangedtopointtoanotherobject.However,theobjectitselfcanbemodified;Javadoesnotprovideawaytomakeanyarbitraryobjectaconstant.(Youcan,however,writeyourclasssothatobjectshavetheeffectofbeingconstant.)Thisrestrictionincludesarrays,whicharealsoobjects.
ØFinalmethods
Therearetworeasonsforfinalmethods.Thefirstistoputa“lock”onthemethodtopreventanyinheritingclassfromchangingitsmeaning.Thisisdonefordesignreasonswhenyouwanttomakesurethatamethod’sbehaviorisretainedduringinheritanceandcannotbeoverridden.
Thesecondreasonforfinalmethodsisefficiency.Ifyoumakeamethodfinal,youareallowingthecompilertoturnanycallstothatmethodintoinlinecalls.Whenthecompilerseesafinalmethodcall,itcan(atitsdiscretion)skipthenormalapproachofinsertingcodetoperformthemethodcallmechanism(pushargumentsonthestack,hopovertothemethodcodeandexecuteit,hopbackandcleanoffthestackarguments,anddealwiththereturnvalue)andinsteadreplacethemethodcallwithacopyoftheactualcodeinthemethodbody.Thiseliminatestheoverheadofthemethodcall.Ofcourse,ifamethodisbig,thenyourcodebeginstobloat,andyouprobablywon’tseeanyperformancegainsfrominlining,sinceanyimprovementswillbedwarfedbytheamountoftimespentinsidethemethod.ItisimpliedthattheJavacompilerisabletodetectthesesituationsandchoosewiselywhethertoinlineafinalmethod.However,it’sbesttoletthecompilerandJVMhandleefficiencyissuesandmakeamethodfinalonlyifyouwanttoexplicitlypreventoverriding
ØFinalclasses
Whenyousaythatanentireclassisfinal(byprecedingitsdefinitionwiththefinalkeyword),youstatethatyoudon’twanttoinheritfromthisclassorallowanyoneelsetodoso.Inotherwords,forsomereasonthedesignofyourclassissuchthatthereisneveraneedtomakeanychanges,orforsafetyorsecurityreasonsyoudon’twantsubclassing
Notethatthefieldsofafinalclasscanbefinalornot,asyouchoose.ThesamerulesapplytofinalforfieldsregardlessofwhetHowever,becauseitpreventsinheritance,allmethodsinafinalclassareimplicitlyfinal,sincethere’snowaytooverridethem.Youcanaddthefinalspecifiertoamethodinafinalclass,butitdoesn’taddanymeaning.hertheclassisdefinedasfinal.
ØSummary
Bothinheritanceandcompositionallowyoutocreateanewtypefromexistingtypes.Typically,however,compositionreusesexistingtypesaspartoftheunderlyingimplementationofthenewtype,andinheritancereusestheinterface.Sincethederivedclasshasthebase-classinterface,itcanbeupcasttothebase,whichiscriticalforpolymorphism,asyou’llseeinthenextchapter.
Despitethestrongemphasisoninheritanceinobject-orientedprogramming,whenyoustartadesignyoushouldgenerallyprefercompositionduringthefirstcutanduseinheritanceonlywhenitisclearlynecessary.Compositiontendstobemoreflexible.Inaddition,byusingtheaddedartificeofinheritancewithyourmembertype,youcanchangetheexacttype,andthusthebehavior,ofthosememberobjectsatruntime.Therefore,youcanchangethebehaviorofthecomposedobjectatruntime.
Whendesigningasystem,yourgoalistofindorcreateasetofclassesinwhicheachclasshasaspecificuseandisneithertoobig(encompassingsomuchfunctionalitythatit’sunwieldytoreuse)norannoyinglysmall(youcan’tuseitbyitselforwithoutaddingfunctionality).
类
“Java
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 软件工程 外文 文献 翻译