ZYBS网上书店英文文献Word格式文档下载.docx
《ZYBS网上书店英文文献Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《ZYBS网上书店英文文献Word格式文档下载.docx(10页珍藏版)》请在冰豆网上搜索。
![ZYBS网上书店英文文献Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-11/29/c7b8e374-37f5-4d70-8b80-b8369f47e491/c7b8e374-37f5-4d70-8b80-b8369f47e4911.gif)
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.Thefollowingfigureillustratesa
bicyclemodeledasasoftwareobject.
Abicyclemodeledasasoftware
object.
Inadditiontoitsvariables,thesoftwarebicyclewouldalsohavemethodstobrake,changethepedalcadence,andchangegears.(Itwouldnothaveamethodforchangingitsspeedbecausethebike'
sspeedisjustasideeffectofwhichgearit'
sinandhowfasttheriderispedaling.)Thesemethodsareknownformallyasinstancemethodsbecausetheyinspectorchangethestateofaparticularbicycleinstance.
Objectdiagramsshowthatanobject'
svariablesmakeupthecenter,ornucleus,oftheobject.Methodssurroundandhidetheobject'
snucleusfromotherobjectsintheprogram.Packaginganobject'
svariableswithintheprotectivecustodyofitsmethodsiscalledencapsulation.Thisconceptualpictureofanobject—
anucleusofvariablespackagedwithinaprotectivemembraneofmethods—is
anidealrepresentationofanobjectandistheidealthatdesignersofobject-orientedsystemsstrivefor.However,it'
snotthewholestory.Often,forpracticalreasons,anobjectmayexposesomeofitsvariablesorhidesomeofitsmethods.IntheJavaprogramminglanguage,anobjectcanspecifyoneoffouraccesslevelsforeachofitsvariablesandmethods.Theaccesslevel
determineswhichotherobjectsandclassescanaccessthatvariableormethod.RefertotheControllingAccesstoMembersofaClasssectionfordetails.Encapsulatingrelatedvariablesandmethodsintoaneatsoftwarebundleisasimpleyetpowerfulideathatprovidestwoprimarybenefitstosoftwaredevelopers:
Modularity:
Thesourcecodeforanobjectcanbewrittenandmaintained
independentlyofthesourcecodeforotherobjects.Also,anobject
canbeeasilypassedaroundinthesystem.Youcangiveyourbicycle
tosomeoneelse,anditwillstillwork.
Information-hiding:
Anobjecthasapublicinterfacethatother
objectscanusetocommunicatewithit.Theobjectcanmaintainprivate
informationandmethodsthatcanbechangedatanytimewithout
affectingotherobjectsthatdependonit.Youdon'
tneedtounderstand
abike'
sgearmechanismtouseit.
WhatIsaMessage?
Asingleobjectalonegenerallyisnotveryuseful.Instead,anobjectusuallyappearsasacomponentofalargerprogramorapplicationthatcontainsmanyotherobjects.Throughtheinteractionoftheseobjects,programmersachievehigher-orderfunctionalityandmorecomplexbehavior.Yourbicyclehangingfromahookinthegarageisjustabunchofmetalandrubber;
byitself,itisincapableofanyactivity;
thebicycleisusefulonlywhenanotherobject(you)interactswithit(bypedaling).
Softwareobjectsinteractandcommunicatewitheachotherbysendingmessages
toeachother.WhenobjectAwantsobjectBtoperformoneofB'
smethods,objectAsendsamessagetoobjectB(seethefollowingfigure).
Objectsinteractbysendingeachothermessages.
Sometimes,thereceivingobjectneedsmoreinformationsothatitknowsexactlywhattodo;
forexample,whenyouwanttochangegearsonyourbicycle,youhavetoindicatewhichgearyouwant.Thisinformationispassedalongwiththemessageasparameters.
Messagesuseparameterstopassalong
extrainformationthattheobject
needs—inthiscase,whichgearthe
bicycleshouldbein.
Thesethreepartsareenoughinformationforthereceivingobjecttoperformthedesiredmethod.Nootherinformationorcontextisrequired.
Messagesprovidetwoimportantbenefits:
Anobject'
sbehaviorisexpressedthroughitsmethods,so(asidefrom
directvariableaccess)messagepassingsupportsallpossible
interactionsbetweenobjects.
Objectsdon'
tneedtobeinthesameprocessorevenonthesamemachine
tosendmessagesbackandforthandreceivemessagesfromeachother.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.
Aclassisablueprintthatdefinesthevariablesandthemethodscommontoallobjectsofacertainkind.
Theclassforourbicycleexamplewoulddeclaretheinstancevariablesnecessarytocontainthecurrentgear,thecurrentcadence,andsoonforeachbicycleobject.Theclasswouldalsodeclareandprovideimplementationsfortheinstancemethodsthatallowtheridertochangegears,brake,andchangethepedalingcadence,asshowninthenextfigure.
Thebicycleclass.
Afteryou'
vecreatedthebicycleclass,youcancreateanynumberofbicycleobjectsfromthatclass.Whenyoucreateaninstanceofaclass,thesystemallocatesenoughmemoryfortheobjectandallitsinstancevariables.Eachinstancegetsitsowncopyofalltheinstancevariablesdefinedintheclass,asthenextfigureshows.
MyBikeandYourBikearetwodifferentinstancesoftheBikeclass.Eachinstance
hasitsowncopyoftheinstancevariablesdefinedintheBikeclassbuthas
differentvaluesforthesevariables.
Inadditiontoinstancevariables,classescandefineclassvariables.Aclasswariablecontainsinformationthatissharedbyallinstancesoftheclass.Forexample,supposethatallbicycleshadthesamenumberofgears.Inthiscase,defininganinstancevariabletoholdthenumberofgearsisinefficient;
eachinstancewouldhaveitsowncopyofthevariable,butthevaluewouldbethesameforeveryinstance.Insuchsituations,youcandefineaclassvariablethatcontainsthenumberofgears(seethefollowingfigure);
allinstancessharethisvariable.Ifoneobjectchangesthevariable,itchangesforallotherobjectsofthattype.
YourBike,aninstanceofBike,hasaccesstothenumberOfGearsvariableinthe
Bikeclass;
however,theYourBikeinstancedoesnothaveacopyofthisclass
variable.
AclasscanalsodeclareclassmethodsYoucaninvokeaclassmethoddirectlyfromtheclass,whereasyoumustinvokeinstancemethodsonaparticularinstance.TheUnderstandingInstanceandClassMemberssectiondiscussesinstancevariablesandmethodsandclassvariablesandmethodsindetail.Objectsprovidethebenefitofmodularityandinformation-hiding.Classesprovidethebenefitofreusability.Bicyclemanufacturersusethesameblueprintoverandoveragaintobuildlotsofbicycles.Softwareprogrammersusethesameclass,andthusthesamecode,overandoveragaintocreatemanyobjects.ObjectsversusClasses
You'
veprobablynoticedthattheillustrationsofobjectsandclasseslookverysimilar.Andindeed,thedifferencebetweenclassesandobjectsisoftenthesourceofsomeconfusion.Intherealworld,it'
sobviousthatclassesarenotthemselvestheobjectstheydescribe;
thatis,ablueprintofabicycleisnota