shavestate(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.Theclasswould
上一篇:
ASP_net电子购物商城系统+论文(功能结构图、业务流程图、数据流程图、数据字典)(论文和程序)
下一篇:
试析影响公路路面平整度的因素及应采取的施工措施