计算机专业毕业设计论文外文文献中英文翻译Object.docx
- 文档编号:11227566
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:12
- 大小:25.74KB
计算机专业毕业设计论文外文文献中英文翻译Object.docx
《计算机专业毕业设计论文外文文献中英文翻译Object.docx》由会员分享,可在线阅读,更多相关《计算机专业毕业设计论文外文文献中英文翻译Object.docx(12页珍藏版)》请在冰豆网上搜索。
计算机专业毕业设计论文外文文献中英文翻译Object
外文资料
Objectlandscapesandlifetimes
Technically,OOPisjustaboutabstractdatatyping,inheritance,andpolymorphism,butotherissuescanbeatleastasimportant.Theremainderofthissectionwillcovertheseissues.
Oneofthemostimportantfactorsisthewayobjectsarecreatedanddestroyed.Whereisthedataforanobjectandhowisthelifetimeoftheobjectcontrolled?
Therearedifferentphilosophiesatworkhere.C++takestheapproachthatcontrolofefficiencyisthemostimportantissue,soitgivestheprogrammerachoice.Formaximumrun-timespeed,thestorageandlifetimecanbedeterminedwhiletheprogramisbeingwritten,byplacingtheobjectsonthestack(thesearesometimescalledautomaticorscopedvariables)orinthestaticstoragearea.Thisplacesapriorityonthespeedofstorageallocationandrelease,andcontrolofthesecanbeveryvaluableinsomesituations.However,yousacrificeflexibilitybecauseyoumustknowtheexactquantity,lifetime,andtypeofobjectswhileyou'rewritingtheprogram.Ifyouaretryingtosolveamoregeneralproblemsuchascomputer-aideddesign,warehousemanagement,orair-trafficcontrol,thisistoorestrictive.
Thesecondapproachistocreateobjectsdynamicallyinapoolofmemorycalledtheheap.Inthisapproach,youdon'tknowuntilrun-timehowmanyobjectsyouneed,whattheirlifetimeis,orwhattheirexacttypeis.Thosearedeterminedatthespurofthemomentwhiletheprogramisrunning.Ifyouneedanewobject,yousimplymakeitontheheapatthepointthatyouneedit.Becausethestorageismanageddynamically,atrun-time,theamountoftimerequiredtoallocatestorageontheheapissignificantlylongerthanthetimetocreatestorageonthestack.(Creatingstorageonthestackisoftenasingleassemblyinstructiontomovethestackpointerdown,andanothertomoveitbackup.)Thedynamicapproachmakesthegenerallylogicalassumptionthatobjectstendtobecomplicated,sotheextraoverheadoffindingstorageandreleasingthatstoragewillnothaveanimportantimpactonthecreationofanobject.Inaddition,thegreaterflexibilityisessentialtosolvethegeneralprogrammingproblem.
Javausesthesecondapproach,exclusively].Everytimeyouwanttocreateanobject,youusethenewkeywordtobuildadynamicinstanceofthatobject.
There'sanotherissue,however,andthat'sthelifetimeofanobject.Withlanguagesthatallowobjectstobecreatedonthestack,thecompilerdetermineshowlongtheobjectlastsandcanautomaticallydestroyit.However,ifyoucreateitontheheapthecompilerhasnoknowledgeofitslifetime.InalanguagelikeC++,youmustdetermineprogrammaticallywhentodestroytheobject,whichcanleadtomemoryleaksifyoudon’tdoitcorrectly(andthisisacommonprobleminC++programs).Javaprovidesafeaturecalledagarbagecollectorthatautomaticallydiscoverswhenanobjectisnolongerinuseanddestroysit.Agarbagecollectorismuchmoreconvenientbecauseitreducesthenumberofissuesthatyoumusttrackandthecodeyoumustwrite.Moreimportant,thegarbagecollectorprovidesamuchhigherlevelofinsuranceagainsttheinsidiousproblemofmemoryleaks(whichhasbroughtmanyaC++projecttoitsknees).
Therestofthissectionlooksatadditionalfactorsconcerningobjectlifetimesandlandscapes.
1Collectionsanditerators
Ifyoudon’tknowhowmanyobjectsyou’regoingtoneedtosolveaparticularproblem,orhowlongtheywilllast,youalsodon’tknowhowtostorethoseobjects.Howcanyouknowhowmuchspacetocreateforthoseobjects?
Youcan’t,sincethatinformationisn’tknownuntilrun-time.
Thesolutiontomostproblemsinobject-orienteddesignseemsflippant:
youcreateanothertypeofobject.Thenewtypeofobjectthatsolvesthisparticularproblemholdsreferencestootherobjects.Ofcourse,youcandothesamethingwithanarray,whichisavailableinmostlanguages.Butthere’smore.Thisnewobject,generallycalledacontainer(alsocalledacollection,buttheJavalibraryusesthatterminadifferentsensesothisbookwilluse“container”),willexpanditselfwhenevernecessarytoaccommodateeverythingyouplaceinsideit.Soyoudon’tneedtoknowhowmanyobjectsyou’regoingtoholdinacontainer.Justcreateacontainerobjectandletittakecareofthedetails.
Fortunately,agoodOOPlanguagecomeswithasetofcontainersaspartofthepackage.InC++,it’spartoftheStandardC++LibraryandissometimescalledtheStandardTemplateLibrary(STL).ObjectPascalhascontainersinitsVisualComponentLibrary(VCL).Smalltalkhasaverycompletesetofcontainers.Javaalsohascontainersinitsstandardlibrary.Insomelibraries,agenericcontainerisconsideredgoodenoughforallneeds,andinothers(Java,forexample)thelibraryhasdifferenttypesofcontainersfordifferentneeds:
avector(calledanArrayListinJava)forconsistentaccesstoallelements,andalinkedlistforconsistentinsertionatallelements,forexample,soyoucanchoosetheparticulartypethatfitsyourneeds.Containerlibrariesmayalsoincludesets,queues,hashtables,trees,stacks,etc.
Allcontainershavesomewaytoputthingsinandgetthingsout;thereareusuallyfunctionstoaddelementstoacontainer,andotherstofetchthoseelementsbackout.Butfetchingelementscanbemoreproblematic,becauseasingle-selectionfunctionisrestrictive.Whatifyouwanttomanipulateorcompareasetofelementsinthecontainerinsteadofjustone?
Thesolutionisaniterator,whichisanobjectwhosejobistoselecttheelementswithinacontainerandpresentthemtotheuseroftheiterator.Asaclass,italsoprovidesalevelofabstraction.Thisabstractioncanbeusedtoseparatethedetailsofthecontainerfromthecodethat’saccessingthatcontainer.Thecontainer,viatheiterator,isabstractedtobesimplyasequence.Theiteratorallowsyoutotraversethatsequencewithoutworryingabouttheunderlyingstructure—thatis,whetherit’sanArrayList,aLinkedList,aStack,orsomethingelse.Thisgivesyoutheflexibilitytoeasilychangetheunderlyingdatastructurewithoutdisturbingthecodeinyourprogram.Javabegan(inversion1.0and1.1)withastandarditerator,calledEnumeration,forallofitscontainerclasses.Java2hasaddedamuchmorecompletecontainerlibrarythatcontainsaniteratorcalledIteratorthatdoesmorethantheolderEnumeration.
Fromadesignstandpoint,allyoureallywantisasequencethatcanbemanipulatedtosolveyourproblem.Ifasingletypeofsequencesatisfiedallofyourneeds,there’dbenoreasontohavedifferentkinds.Therearetworeasonsthatyouneedachoiceofcontainers.First,containersprovidedifferenttypesofinterfacesandexternalbehavior.Astackhasadifferentinterfaceandbehaviorthanthatofaqueue,whichisdifferentfromthatofasetoralist.Oneofthesemightprovideamoreflexiblesolutiontoyourproblemthantheother.Second,differentcontainershavedifferentefficienciesforcertainoperations.ThebestexampleisanArrayListandaLinkedList.Botharesimplesequencesthatcanhaveidenticalinterfacesandexternalbehaviors.Butcertainoperationscanhaveradicallydifferentcosts.RandomlyaccessingelementsinanArrayListisaconstant-timeoperation;ittakesthesameamountoftimeregardlessoftheelementyouselect.However,inaLinkedListitisexpensivetomovethroughthelisttorandomlyselectanelement,andittakeslongertofindanelementthatisfurtherdownthelist.Ontheotherhand,ifyouwanttoinsertanelementinthemiddleofasequence,it’smuchcheaperinaLinkedListthaninanArrayList.Theseandotheroperationshavedifferentefficienciesdependingontheunderlyingstructureofthesequence.Inthedesignphase,youmightstartwithaLinkedListand,whentuningforperformance,changetoanArrayList.Becauseoftheabstractionviaiterators,youcanchangefromonetotheotherwithminimalimpactonyourcode.
Intheend,rememberthatacontainerisonlyastoragecabinettoputobjectsin.Ifthatcabinetsolvesallofyourneeds,itdoesn’treallymatterhowitisimplemented(abasicconceptwithmosttypesofobjects).Ifyou’reworkinginaprogrammingenvironmentthathasbuilt-inoverheadduetootherfactors,thenthecostdifferencebetweenanArrayListandaLinkedListmightnotmatter.Youmightneedonlyonetypeofsequence.Youcanevenimaginethe“perfect”containerabstraction,whichcanautomaticallychangeitsunderlyingimplementationaccordingtothewayitisused.
2Thesinglyrootedhierarchy
OneoftheissuesinOOPthathasbecomeespeciallyprominentsincetheintroductionofC++iswhetherallclassesshouldultimatelybeinheritedfromasinglebaseclass.InJava(aswithvirtuallyallotherOOPlanguages)theansweris“yes”andthenameofthisultimatebaseclassissimplyObject.Itturnsoutthatthebenefitsofthesinglyrootedhierarchyaremany.
Allobjectsinasinglyrootedhierarchyhaveaninterfaceincommon,sotheyareallultimatelythesametype.Thealternative(providedbyC++)isthatyoudon’tknowthateverythingisthesamefundamentaltype.Fromabackward-compatibilitystandpointthisfitsthemodelofCbetterandcanbethoughtofaslessrestrictive,butwhenyouwanttodofull-onobject-orientedprogrammingyoumustthenbuildyourownhierarchytoprovidethesameconveniencethat’sbuiltintootherOOPlanguages.Andinanynewclasslibraryyouacquire,someotherincompatibleinterfacewillbeused.Itrequireseffort(andpossiblymultipleinheritance)toworkthenewinterfaceintoyourdesign.Istheextra“flexibility”ofC++worthit?
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机专业 毕业设计 论文 外文 文献 中英文 翻译 Object