英语文献翻译.docx

上传人:b****1 文档编号:2396448 上传时间:2022-10-29 格式:DOCX 页数:8 大小:25.93KB
下载 相关 举报
英语文献翻译.docx_第1页
第1页 / 共8页
英语文献翻译.docx_第2页
第2页 / 共8页
英语文献翻译.docx_第3页
第3页 / 共8页
英语文献翻译.docx_第4页
第4页 / 共8页
英语文献翻译.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

英语文献翻译.docx

《英语文献翻译.docx》由会员分享,可在线阅读,更多相关《英语文献翻译.docx(8页珍藏版)》请在冰豆网上搜索。

英语文献翻译.docx

英语文献翻译

Objectlandscapesandlifetimes

Technically,OOPisjustaboutabstractdatatype,inheritanceandpolymorphism,butotherissuescanbeatleastasimportant.Theremainderofthissectionwillcovertheseissues.

Oneofthemostimportantfactorsisthewayobjectsarecreatedanddestroyed.Whereisthedataforanobjectandhowisthelifetimeoftheobjectcontrolled?

Therearedifferentphilosophiesatworkhere.C++takestheapproachthatcontrolofefficiencyisthemostimportantissue,soitgivestheprogrammerachoice.Formaximumrun-timespeed,thestorageandlifetimecanbedeterminedwhentheprogramisbeingwritten,byplacingtheobjectsonthestack(thesearesometimescalledautomaticorscopedvariables)orinthestaticstoragearea.Thisplacesapriorityonthespeedofstorageallocationandrelease,andcontrolofthesecanbeveryvaluableinsomesituations.However,yousacrificeflexibilitybecauseyoumustknowtheexactquantity,lifetime,andtypeofobjectswhileyou'rewritingtheprogram.Ifyouaretryingtosolveamoregeneralproblemsuchascomputer-aideddesign,warehousemanagement,orair-trafficcontrol,thisistoorestrictive.

Thesecondapproachistocreateobjectsdynamicallyinapoolofmemorycalledtheheap.Inthisapproach,youdon'tknowhowmanyobjectsyouneed,whattheirlifetimeis,orwhattheirexacttypeisuntilrun-time.Thosearedeterminedatthespurofthemomentwhiletheprogramisrunning.Ifyouneedanewobject,yousimplymakeitontheheapatthepointthatyouneedit.Becausethestorageismanageddynamically,atrun-time,theamountoftimerequiredtoallocatestorageontheheapissignificantlylongerthanthetimetocreatestorageonthestack.(Creatingstorageonthestackisoftenasingleassemblyinstructiontomovethestackpointerdown,andanothertomoveitbackup.)Thedynamicapproachmakesthegenerallylogicalassumptionthatobjectstendtobecomplicated,sotheextraoverheadoffindingstorageandreleasingthatstoragewillnothaveanimportantimpactonthecreationofanobject.Inaddition,thegreaterflexibilityisessentialtosolvethegeneralprogrammingproblem.

C++usesthesecondapproach,exclusively].Everytimeyouwanttocreateanobject,youusethenewkeywordtobuildadynamicinstanceofthatobject.

There'sanotherissue,however,andthat'sthelifetimeofanobject.Withlanguagesthatallowobjectstobecreatedonthestack,thecompilerdetermineshowlongtheobjectlastsandcanautomaticallydestroyit.However,ifyoucreateitontheheapthecompilerhasnoknowledgeofitslifetime.InalanguagelikeC++,youmustdetermineprogrammaticallywhentodestroytheobject,whichcanleadtomemoryleaksifyoudon’tdoitcorrectly(andthisisacommonprobleminC++programs).Javaprovidesafeaturecalledagarbagecollectorthatautomaticallydiscoverswhenanobjectisnolongerinuseanddestroysit.Agarbagecollectorismuchmoreconvenientbecauseitreducesthenumberofissuesthatyoumusttrackandthecodeyoumustwrite.

Therestofthissectionlooksatadditionalfactorsconcerningobjectlandscapesandlifetimes.

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,soyoucanchoosethep

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1