计算机专业英文文献.docx
- 文档编号:23252211
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:10
- 大小:108.93KB
计算机专业英文文献.docx
《计算机专业英文文献.docx》由会员分享,可在线阅读,更多相关《计算机专业英文文献.docx(10页珍藏版)》请在冰豆网上搜索。
计算机专业英文文献
WhatIsanObject?
Objectsarekeytounderstandingobject-orientedtechnology.Youcanlookaroundyounowandseemanyexamplesofreal-worldobjects:
yourdog,yourdesk,yourtelevisionset,yourbicycle.
Real-worldobjectssharetwocharacteristics:
Theyallhavestateandbehavior.Forexample,dogshavestate(name,color,breed,hungry)andbehavior(barking,fetching,waggingtail).Bicycleshavestate(currentgear,currentpedalcadence,twowheels,numberofgears)andbehavior(braking,accelerating,slowingdown,changinggears).
Softwareobjectsaremodeledafterreal-worldobjectsinthattheytoohavestateandbehavior.Asoftwareobjectmaintainsitsstateinoneormorevariables.Avariableisanitemofdatanamedbyanidentifier.Asoftwareobjectimplementsitsbehaviorwithmethods.Amethodisafunction(subroutine)associatedwithanobject.
Definition:
Anobjectisasoftwarebundleofvariablesandrelatedmethods.
Youcanrepresentreal-worldobjectsbyusingsoftwareobjects.Youmightwanttorepresentreal-worlddogsassoftwareobjectsinananimationprogramorareal-worldbicycleasasoftwareobjectintheprogramthatcontrolsanelectronicexercisebike.Youcanalsousesoftwareobjectstomodelabstractconcepts.Forexample,aneventisacommonobjectusedinwindowsystemstorepresenttheactionofauserpressingamousebuttonorakeyonthekeyboard.Thefollowingillustrationisacommonvisualrepresentationofasoftwareobject.
Asoftwareobject.
Everythingthesoftwareobjectknows(state)andcando(behavior)isexpressedbythevariablesandthemethodswithinthatobject.Asoftwareobjectthatmodelsyourreal-worldbicyclewouldhavevariablesthatindicatethebicycle'scurrentstate:
Itsspeedis18mph,itspedalcadenceis90rpm,anditscurrentgearis5th.Thesevariablesareformallyknownasinstancevariablesbecausetheycontainthestateforaparticularbicycleobject;inobject-orientedterminology,aparticularobjectiscalledaninstance.Thefollowingfigureillustratesabicyclemodeledasasoftwareobject.
Abicyclemodeledasasoftwareobject.
Inadditiontoitsvariables,thesoftwarebicyclewouldalsohavemethodstobrake,changethepedalcadence,andchangegears.(Itwouldnothaveamethodforchangingitsspeedbecausethebike'sspeedisjustasideeffectofwhichgearit'sinandhowfasttheriderispedaling.)Thesemethodsareknownformallyasinstancemethodsbecausetheyinspectorchangethestateofaparticularbicycleinstance.
Objectdiagramsshowthatanobject'svariablesmakeupthecenter,ornucleus,oftheobject.Methodssurroundandhidetheobject'snucleusfromotherobjectsintheprogram.Packaginganobject'svariableswithintheprotectivecustodyofitsmethodsiscalledencapsulation.Thisconceptualpictureofanobject—anucleusofvariablespackagedwithinaprotectivemembraneofmethods—isanidealrepresentationofanobjectandistheidealthatdesignersofobject-orientedsystemsstrivefor.However,it'snotthewholestory.
Often,forpracticalreasons,anobjectmayexposesomeofitsvariablesorhidesomeofitsmethods.IntheJavaprogramminglanguage,anobjectcanspecifyoneoffouraccesslevelsforeachofitsvariablesandmethods.Theaccessleveldetermineswhichotherobjectsandclassescanaccessthatvariableormethod.RefertotheControllingAccesstoMembersofaClasssectionfordetails.
Encapsulatingrelatedvariablesandmethodsintoaneatsoftwarebundleisasimpleyetpowerfulideathatprovidestwoprimarybenefitstosoftwaredevelopers:
Modularity:
Thesourcecodeforanobjectcanbewrittenandmaintainedindependentlyofthesourcecodeforotherobjects.Also,anobjectcanbeeasilypassedaroundinthesystem.Youcangiveyourbicycletosomeoneelse,anditwillstillwork.
Information-hiding:
Anobjecthasapublicinterfacethatotherobjectscanusetocommunicatewithit.Theobjectcanmaintainprivateinformationandmethodsthatcanbechangedatanytimewithoutaffectingotherobjectsthatdependonit.Youdon'tneedtounderstandabike'sgearmechanismtouseit.
WhatIsaMessage?
Asingleobjectalonegenerallyisnotveryuseful.Instead,anobjectusuallyappearsasacomponentofalargerprogramorapplicationthatcontainsmanyotherobjects.Throughtheinteractionoftheseobjects,programmersachievehigher-orderfunctionalityandmorecomplexbehavior.Yourbicyclehangingfromahookinthegarageisjustabunchofmetalandrubber;byitself,itisincapableofanyactivity;thebicycleisusefulonlywhenanotherobject(you)interactswithit(bypedaling).
Softwareobjectsinteractandcommunicatewitheachotherbysendingmessagestoeachother.WhenobjectAwantsobjectBtoperformoneofB'smethods,objectAsendsamessagetoobjectB(seethefollowingfigure).
Objectsinteractbysendingeachothermessages.
Sometimes,thereceivingobjectneedsmoreinformationsothatitknowsexactlywhattodo;forexample,whenyouwanttochangegearsonyourbicycle,youhavetoindicatewhichgearyouwant.Thisinformationispassedalongwiththemessageasparameters.
Messagesuseparameterstopassalongextrainformationthattheobjectneeds—inthiscase,whichgearthebicycleshouldbein.
Thesethreepartsareenoughinformationforthereceivingobjecttoperformthedesiredmethod.Nootherinformationorcontextisrequired.
Messagesprovidetwoimportantbenefits:
Anobject'sbehaviorisexpressedthroughitsmethods,so(asidefromdirectvariableaccess)messagepassingsupportsallpossibleinteractionsbetweenobjects.
Objectsdon'tneedtobeinthesameprocessorevenonthesamemachinetosendmessagesbackandforthandreceivemessagesfromeachother.
WhatIsaClass?
Intherealworld,youoftenhavemanyobjectsofthesamekind.Forexample,yourbicycleisjustoneofmanybicyclesintheworld.Usingobject-orientedterminology,wesaythatyourbicycleobjectisaninstanceoftheclassofobjectsknownasbicycles.Bicycleshavesomestate(currentgear,currentcadence,twowheels)andbehavior(changegears,brake)incommon.However,eachbicycle'sstateisindependentofandcanbedifferentfromthatofotherbicycles.
Whenbuildingthem,manufacturerstakeadvantageofthefactthatbicyclessharecharacteristics,buildingmanybicyclesfromthesameblueprint.Itwouldbeveryinefficienttoproduceanewblueprintforeverybicyclemanufactured.
Inobject-orientedsoftware,it'salsopossibletohavemanyobjectsofthesamekindthatsharecharacteristics:
rectangles,employeerecords,videoclips,andsoon.Likebicyclemanufacturers,youcantakeadvantageofthefactthatobjectsofthesamekindaresimilarandyoucancreateablueprintforthoseobjects.Asoftwareblueprintforobjectsiscalledaclass(seethefollowingfigure).
Avisualrepresentationofaclass.
Definition:
Aclassisablueprintthatdefinesthevariablesandthemethodscommontoallobjectsofacertainkind.
Theclassforourbicycleexamplewoulddeclaretheinstancevariablesnecessarytocontainthecurrentgear,thecurrentcadence,andsoonforeachbicycleobject.Theclasswouldalsodeclareandprovideimplementationsfortheinstancemethodsthatallowtheridertochangegears,brake,andchangethepedalingcadence,asshowninthenextfigure.
Thebicycleclass.
Afteryou'vecreatedthebicycleclass,youcancreateanynumberofbicycleobjectsfromthatclass.Whenyoucreateaninstanceofaclass,thesystemallocatesenoughmemoryfortheobjectandallitsinstancevariables.Eachinstancegetsitsowncopyofalltheinstancevariablesdefinedintheclass,asthenextfigureshows.
MyBikeandYourBikearetwodifferentinstancesoftheBikeclass.EachinstancehasitsowncopyoftheinstancevariablesdefinedintheBikeclassbuthasdifferentvaluesforthesevariables.
Inadditiontoinstancevariables,classescandefineclassvariables.Aclasswariablecontainsinformationthatissharedbyallinstancesoftheclass.Forexample,supposethatallbicycleshadthesamenumberofgears.Inthiscase,defininganinstancevariabletoholdthenumberofgearsisinefficient;eachinstancewouldhaveitsowncopyofthevariable,butthevaluewouldbethesameforeveryinstance.Insuchsituations,youcandefineaclassvariablethatcontainsthenumberofgears(seethefollowingfigure);allinstancessharethisvariable.Ifoneobjectchangesthevariable,itchangesforallotherobjectsofthattype.
YourBike,aninstanceofBike,hasaccesstothenumberOfGearsvariableintheBikeclass;however,theYourBikeinstancedoesnothaveacopyofthisclassvariable.
AclasscanalsodeclareclassmethodsYoucaninvokeaclassmethoddirectlyfromtheclass,whereasyoumustinvokeinstancemethodsonaparticularinstance.
TheUnderstandingInstanceandClassMemberssectiondiscussesinstancevariablesandmethodsandclassvariablesandmethodsindetail.
Objectsprovidethebenefitofmodularityandinformation-hiding.Classesprovidethebenefitofreusability.Bicyclemanufacturersusethesameblueprintoverandoveragaintobuildlotsofbicycles.Softwareprogrammersusethesameclass,andthusthesamecode,overandoveragaintocreatemanyobjects.
ObjectsversusClasses
You'veprobablynoticedthattheillustrationsofobjectsandclasseslookverysimilar.Andindeed,thedifferencebetweenclassesandobjectsisoftenthesourceofsomeconfusion.Intherealworld,it'sobviousthatclassesarenotthemselvestheobjectstheydescribe;thatis,ablueprintofabicycleisnotabicycle.However,it'salittlemoredifficultto
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机专业 英文 文献
![提示](https://static.bdocx.com/images/bang_tan.gif)