面向对象的程序设计01

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

0/44Chap.1Object-OrientedProgramming1/44Content1.1Object-OrientedandProceduralProgramming1.2ClassesandAbstractDataTypes1.3TheClient/ServerModelandMessagePassing1.4InheritanceandPolymorphism1.5InterfacesandComponents1.6Template2/441.1Object-OrientedandProceduralProgramming3/44程序设计范型(ProgrammingParadigms/style)▪程序设计范型—程序设计时所采用的基本方式模型。它决定了在程序设计时的思维方式及所采用的工具,它受应用领域的约束,以某一类程序设计语言为基础。▪程序设计风格(ProgrammingStyles)通常由思维方式和语言支持所决定,而不同的领域要求的思维方式不同,因而程序设计风格不同,对语言的支持要求也不同。4/44ProceduralProgramming(面向过程编程)Decidewhichproceduresyouwant;(程序设计的核心)usethebestalgorithmsyoucanfind.(过程的内涵)以功能为中心,基于功能分解的程序设计范型程序可以被看作为函数调用序列(图中没有画出三级函数)mainprogramdataProcedure1Procedure2Procedure35/44ProceduralProgrammingProceduralprogrammingisassociatedwithadesigntechniqueknowsastop-downdesign(自顶向下).BuildChassisBuildEngineBuildDriveTrainAssembleInspectBuildanautomobileMainProblemProblemdecomposition问题分解6/44ProceduralProgrammingIntop-downdesign,aproblemisassociatedwithaprocedure.BuildChassisBuildEngineBuildDriveTrainAssembleInspectMainProblembuildC()buildE()buildD()assemble()inspect()main()voidbuildC(){…}……voidinspect(){…}intmain(){buildC();buildE();buildD();assemble();inspect();……}7/44Object-OrientedProgramming(OOP)动物界软体动物门脊索动物门爬行纲哺乳纲食肉目灵长目人科人属人种界门纲目科属种角龙科林奈物种继承层次图8/44Object-OrientedProgrammingDecidewhichclassesyouwant;provideafullsetofoperationsforeachclass;makecommonality(共性)explicitbyusinginheritance(继承).9/44Object-OrientedProgrammingClass(类)是对一组类似事物的共同属性和行为特征的抽象描述.Objects(对象)inaclassshareproperties,features,orattributes.InC++,theseattributesarecalleddatamembers(数据成员).Aclasshasactionsorprocessesassociatedwithit.Objectsinaclasssharetheseactions.InC++,theseactionsarecalledfunctionmembers(成员函数)ormethods(方法).10/44Object-OrientedProgrammingRelationships(关系)HumannoseeyesworkhasaHumanAnimalisaMaryLeakeyinstanceof11/441.2ClassesandAbstractDataTypes12/44InformationHiding(信息隐蔽)Theimplementationishiddenfromtheuser,whereastheinterfaceisexposed(暴露)totheuser.Inanobject-orientedlanguage,aclassisamodulethatsupportsinformationhiding.InaC++class,wecanusekeywordssuchaspublicandprivatetocontrolaccess(存取)totheclass’spropertiesandoperations.13/44Encapsulation(封装)Thestandardmechanismforproceduralprogrammingofdatamanipulation(操作)ispassingargumentstoandreturningavaluefromafunction.Inobject-orientedprogramming,dataandtheprocedurestomanipulatethedatacanbeencapsulated(封装)orcontainedwithinaclass.Atypicalroleoftheclass’sencapsulateddatamembersistosupportitsencapsulatedmethods.14/44AbstractDataTypes(抽象数据类型,ADT)ADT:exposespublicinterface,hidesimplementationdetails.ADT:值集+操作集classRECT{private:doubletop,left;doublebottom,right;public:voidinit(double,double,double,double);voiddraw();voidmove(doublex,doubley);};优点:•模块化程度高•密封性可保证数据完整•实现部分可独立更换15/44Class(类)Classisauser-definedtype(用户自定义类型).Atypeisaconcreterepresentationofaconceptinapplicationdomain(领域).Wedesignanewtypetoprovideadefinitionofaconceptthathasnodirectcounterpart(复本)amongthebuilt-in(内建、内置)types.16/441.3Client/ServerModelAclientrequestsservicesfromaServerobjectbyinvokingoneofitsmethods,whichischaracterizedassendingamessagetotheobject.ClientServerGET:ServerS;intret=S.GET(“”);17/441.4InheritanceandPolymorphismInheritanceisaconvenientandefficientwaytospecialize(特化)orextend(扩展)aparentclassbycreatingachildclass.HumanAnimalBaseclassDerivedclassInheritanceSuper/ParentclassSubclassisa18/441.4InheritanceandPolymorphismInheritanceallowsthedesignertocombinefeaturesofclassesinacommonparentclass.Inheritancesupportsaformofcodereuse(代码重用).InformationDialogMenuWindowDialogWindowEditWindowQuestionDialogWindowInheritancehierarchy19/441.4InheritanceandPolymorphism(多态)singleinheritance(单继承):achildclasshasexactlyonedirectparent.multipleinheritance(多继承):achildclassmayhavemultipledirectparents.InformationDialogMenuWindowDialogWindowEditWindowQuestionDialogWindowInformationDialogMenuWindowDialogWindowEditWindowQuestionDialogWindowManagerTemporaryConsultantWorker20/441.4InheritanceandPolymorphismPolymorphism:Inanobject-orientedlanguage,therecanbemanymethodswiththesamesignature.静态绑定:Ex.inti1=10+5;intadd(int,int);doubled2=10.2+5.0;doubleadd(double,double);21/441.4InheritanceandPolymorphismIt’scommonforeachclassinthehierarchytoprovideitsownappropriateversionofapolymorphicmethod.动态绑定:Ex.22/441.5InterfaceandComponents(接口和组件)Aclass’sinterfaceconsistsofwhatitexposes.Abstractbaseclass(抽象类)23/44有时,即使是用有向无环图(directedacyclicgraph)也不足以组织一个程序中的概念,因为有些概念看上去是固有地相互依赖着。这时,我们应当试着将这样的环形依赖限制在程序的局部,以免使它们影响整个程序的结构。化解依赖图最好的工具之一,是明确地分离接口与实现,C++的抽象类(AbstractClasses)就是这样的工具。例:母鸡鸡蛋母鸡是鸡蛋孵化出来的鸡蛋是母鸡产的Abstractbaseclass(抽象类)24/441.5InterfaceandComponents(接口和组件)C++usesabstractbaseclass(抽象类)tospecifyaninterfaceasacollectionofpublic,high-levelmethods.classstdFile{public:virtualvoidOpen(constchar*name,intmode)=0;virtualvoidClose()=0;virtualvoidRewind()=0;};25/441.5InterfaceandComponents(接口和组件)Asoftwarecomponentisapre-builtpartthatcanbecombinedwithothersuchpartstobuildanapplication(应用程序).Acomponentisplacedinacontaine

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功