ZYBS网上书店英文文献.docx

上传人:b****5 文档编号:4388569 上传时间:2022-12-01 格式:DOCX 页数:10 大小:123.26KB
下载 相关 举报
ZYBS网上书店英文文献.docx_第1页
第1页 / 共10页
ZYBS网上书店英文文献.docx_第2页
第2页 / 共10页
ZYBS网上书店英文文献.docx_第3页
第3页 / 共10页
ZYBS网上书店英文文献.docx_第4页
第4页 / 共10页
ZYBS网上书店英文文献.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

ZYBS网上书店英文文献.docx

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

ZYBS网上书店英文文献.docx

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

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

当前位置:首页 > 经管营销 > 生产经营管理

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

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