ZYBS网上书店英文文献.docx
《ZYBS网上书店英文文献.docx》由会员分享,可在线阅读,更多相关《ZYBS网上书店英文文献.docx(10页珍藏版)》请在冰豆网上搜索。
ZYBS网上书店英文文献
ZYBS网上书店英文文献
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.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.
Definition:
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