面向对象的程序设计_第1页
面向对象的程序设计_第2页
面向对象的程序设计_第3页
面向对象的程序设计_第4页
面向对象的程序设计_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、0,Chap.1 Object-Oriented Programming,1,Content,1.1 Object-Oriented and Procedural Programming 1.2 Classes and Abstract Data Types 1.3 The Client/Server Model and Message Passing 1.4 Inheritance and Polymorphism 1.5 Interfaces and Components 1.6 Template,2,1.1 Object-Oriented and Procedural Programmi

2、ng,3,程序设计范型 ( Programming Paradigms/style ) 程序设计范型程序设计时所采用的基本方式模型。它决定了在程序设计时的思维方式及所采用的工具,它受应用领域的约束,以某一类程序设计语言为基础。 程序设计风格(Programming Styles)通常由思维方式和语言支持所决定,而不同的领域要求的思维方式不同,因而程序设计风格不同,对语言的支持要求也不同。,4,Procedural Programming(面向过程编程),Decide which procedures you want; (程序设计的核心) use the best algorithms you

3、 can find.(过程的内涵),以功能为中心,基于功能分解的程序设计范型,程序可以被看作为函数调用序列(图中没有画出三级函数),main program data,Procedure 1,Procedure 2,Procedure 3,5,Procedural Programming,Procedural programming is associated with a design technique knows as top-down design(自顶向下).,Build an automobile,Problem decomposition 问题分解,6,Procedural Pr

4、ogramming,In top-down design, a problem is associated with a procedure.,void buildC() void inspect() int main() buildC(); buildE(); buildD(); assemble(); inspect(); ,7,Object-Oriented Programming (OOP),动物界,软体动物门,脊索动物门,爬行纲,哺乳纲,食肉目,灵长目,人科,人属,人种,界,门,纲,目,科,属,种,角龙科,林奈物种继承层次图,8,Object-Oriented Programming

5、,Decide which classes you want; provide a full set of operations for each class; make commonality(共性) explicit by using inheritance(继承).,9,Object-Oriented Programming,Class(类) 是对一组类似事物的共同属性和行为特征的抽象描述. Objects(对象) in a class share properties, features, or attributes. In C+, these attributes are calle

6、d data members(数据成员). A class has actions or processes associated with it. Objects in a class share these actions. In C+, these actions are called function members(成员函数) or methods(方法).,10,Object-Oriented Programming,Relationships(关系),Human,Animal,is a,Mary Leakey,instance of,11,1.2 Classes and Abst

7、ract Data Types,12,Information Hiding(信息隐蔽),The implementation is hidden from the user, whereas the interface is exposed(暴露) to the user. In an object-oriented language, a class is a module that supports information hiding. In a C+ class, we can use keywords such as public and private to control acc

8、ess (存取) to the classs properties and operations.,13,Encapsulation(封装),The standard mechanism for procedural programming of data manipulation (操作) is passing arguments to and returning a value from a function. In object-oriented programming, data and the procedures to manipulate the data can be enca

9、psulated (封装) or contained within a class. A typical role of the classs encapsulated data members is to support its encapsulated methods.,14,Abstract Data Types(抽象数据类型,ADT),ADT: exposes public interface, hides implementation details. ADT: 值集 操作集,class RECT private: double top, left; double bottom, r

10、ight; public: void init( double, double, double, double ); void draw(); void move( double x, double y); ;,优点: 模块化程度高 密封性可保证数据完整 实现部分可独立更换,15,Class(类),Class is a user-defined type(用户自定义类型). A type is a concrete representation of a concept in application domain(领域). We design a new type to provide a d

11、efinition of a concept that has no direct counterpart(复本) among the built-in(内建、内置) types.,16,1.3 Client/Server Model,A client requests services from a Server object by invoking one of its methods, which is characterized as sending a message to the object.,Client,Server,GET http:/server/,C,S,in C: S

12、erver S; int ret = S.GET ( “http:/server/” );,17,1.4 Inheritance and Polymorphism,Inheritance is a convenient and efficient way to specialize (特化) or extend(扩展) a parent class by creating a child class.,Base class,Derived class,Inheritance,Super/Parent class,Subclass,is a,18,1.4 Inheritance and Poly

13、morphism,Inheritance allows the designer to combine features of classes in a common parent class. Inheritance supports a form of code reuse(代码重用).,InformationDialog,MenuWindow,DialogWindow,EditWindow,QuestionDialog,Window,Inheritance hierarchy,19,1.4 Inheritance and Polymorphism(多态),single inheritan

14、ce(单继承) : a child class has exactly one direct parent. multiple inheritance(多继承) : a child class may have multiple direct parents.,Manager,Temporary,Consultant,Worker,20,1.4 Inheritance and Polymorphism,Polymorphism: In an object-oriented language, there can be many methods with the same signature.,

15、静态绑定: Ex. int i1 = 10 5; int add(int, int); double d2 = 10.2 5.0; double add(double, double);,21,1.4 Inheritance and Polymorphism,Its common for each class in the hierarchy to provide its own appropriate version of a polymorphic method. 动态绑定: Ex.,22,1.5 Interface and Components(接口和组件),A classs inter

16、face consists of what it exposes. Abstract base class(抽象类),23,有时,即使是用有向无环图(directed acyclic graph)也不足以组织一个程序中的概念,因为有些概念看上去是固有地相互依赖着。这时,我们应当试着将这样的环形依赖限制在程序的局部,以免使它们影响整个程序的结构。 化解依赖图最好的工具之一,是明确地分离接口与实现,C+的抽象类(Abstract Classes)就是这样的工具。,Abstract base class(抽象类),24,1.5 Interface and Components(接口和组件),C+ u

17、ses abstract base class(抽象类) to specify an interface as a collection of public, high-level methods.,class stdFile public: virtual void Open(const char* name, int mode) = 0; virtual void Close() = 0; virtual void Rewind() = 0; ;,25,1.5 Interface and Components(接口和组件),A software component is a pre-bui

18、lt part that can be combined with other such parts to build an application (应用程序). A component is placed in a container (容器), a software construct designed to integrate components by handling communications among them. A software component is typically delivered as a binary file and, therefore, its contents are hidden from the programmer who uses it.,26,1.6 Template

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论