设计论文外文文献中英文翻译.docx

上传人:b****3 文档编号:3145360 上传时间:2022-11-18 格式:DOCX 页数:5 大小:21.51KB
下载 相关 举报
设计论文外文文献中英文翻译.docx_第1页
第1页 / 共5页
设计论文外文文献中英文翻译.docx_第2页
第2页 / 共5页
设计论文外文文献中英文翻译.docx_第3页
第3页 / 共5页
设计论文外文文献中英文翻译.docx_第4页
第4页 / 共5页
设计论文外文文献中英文翻译.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

设计论文外文文献中英文翻译.docx

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

设计论文外文文献中英文翻译.docx

设计论文外文文献中英文翻译

皮晨晖软件081班

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.

1.Thesinglyrootedhierarchy

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?

Ifyouneedit—ifyouhavealargeinvestmentinC—it’squitevaluable.Ifyou’restartingfromscratch,otheralternativessuchasJavacanoftenbemoreproductive.

Allobjectsinasinglyrootedhierarchy(suchasJavaprovides)canbeguaranteedtohavecertainfunctionality.Youknowyoucanperformcertainbasicoperationsoneveryobjectinyoursystem.Asinglyrootedhierarchy,alon

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

当前位置:首页 > 职业教育 > 其它

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

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