1 / 82

面向对象程序设计设施

面向对象程序设计设施. 提要. 面向对象程序设计支持要点 一些对象式语言的相关设施. 三个世界. 客观世界 DVD 播放机 What is reality? 复杂性 认识的主观性 问题世界 抽象! Tell me not what you are but what you have A model of a subset of the reality DVD 播放机 计算机(软件)世界 A model of a model of a subset of the reality. Reality. 现实世界.

yamka
Télécharger la présentation

面向对象程序设计设施

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 面向对象程序设计设施

  2. 提要 • 面向对象程序设计支持要点 • 一些对象式语言的相关设施

  3. 三个世界 • 客观世界 DVD播放机 • What is reality? • 复杂性 认识的主观性 • 问题世界 • 抽象!Tell me not what you are but what you have • A model of a subset of the reality DVD播放机 • 计算机(软件)世界 • A model of a model of a subset of the reality Institute of Computer Software Nanjing University

  4. Reality 现实世界 Abstract 问题世界 软件世界 Institute of Computer Software Nanjing University

  5. An object is a machine

  6. Abastracted object Model: Price: Manufacture: Size: start item index forth before after put_right

  7. start forth put_right before after item index An object has an implementation

  8. start forth put_right before after item index Information hidding

  9. 面向对象程序设计 • “面向对象”是一种“世界观”,是一种“方法论” • 面向对象程序设计乃是基于ADT • 主流的面向对象式语言乃是基于“类”Class

  10. 如何完整地理解“类” ? • 两个侧面:类型 vs. 模块 • 类是抽象数据类型的实现 • 类是对象式程序的基本组成单元 • 基于类的面向对象的语言机制的强有力之处在于“类”统一了类型和模块

  11. Class • 一般说类是抽象数据类型的实现 • 抽象数据类型乃是一种规约; • 类是OOPL实现这种类型的设施; • 但是Meyer说:“A class is an abstract data type equipped with a possibly partial implementation.” • Meyer在Eiffel中强调将ADT规约作为类的一部分 • 强调从规约到实现的一致表达和平滑过度 • 类可能只是部分实现 • Deferred and effective class • But why? Institute of Computer Software Nanjing University

  12. Class • Mold and instance • 类自身也能被当作对象么? Institute of Computer Software Nanjing University

  13. 模块与类型的统一 • 模块是软件分解的单元,是语法概念 • 类型是某些动态对象的静态描述,是语义概念 • 传统语言模块类型分离 • 对象语言 统一模块与类型 Institute of Computer Software Nanjing University

  14. The module-type merge • A class is both: • A module • A type • From the module viewpoint: • Set of available services (features or methods). • From the type viewpoint: • Description of set of possible run-time objects (its instances). Institute of Computer Software Nanjing University

  15. 模块与类型的统一 • But How? • Every object is an instance of some class. • Connection: The services of the class, viewed as a module, are the operations applicable to the instances of the class, viewed as a type. • 结合继承,威力更增 • Extension of the modular 模块的扩展 • Specialization of the type 类型的特化 Institute of Computer Software Nanjing University

  16. 面向对象程序设计支持 • 封装 ADT 类 • 继承子类型开闭原则 • 多态动态绑定

  17. 继承 • 继承 作为模块扩展机制,体现开闭原则,较好地支持软件的复用。 • 继承 作为类型特化机制,体现分类方法,更合理地支持子类型实现。 • 继承也是多态和动态绑定机制的基础。

  18. 子类对父类的扩展与特化 • 继承表达的是一种分类演绎的认识方法 • “To program is to understand.” • 特化:包含范围变小,行为特殊化 • 扩展:增加新的操作 • 重定义:改变行为 Institute of Computer Software Nanjing University

  19. The Open-Closed Principle • Modules should be open and closed. • Definitions: • Open module: May be extended. • Closed module: Usable by clients. May be approved, baselined and (if program unit) compiled. • The rationales are complementary: • For closing a module (manager’s perspective): Clients need it now. • For keeping modules open (developer’s perspective): One frequently overlooks aspects of the problem. Institute of Computer Software Nanjing University

  20. The Open-Closed Principle (1) B A C E D Institute of Computer Software Nanjing University

  21. The Open-Closed Principle (2) B A C E D F A’ H I G Institute of Computer Software Nanjing University

  22. The Open-Closed Principle (3) B A C E D F A’ H I G Institute of Computer Software Nanjing University

  23. 继承与类型 • 子类型 • 可替换性:Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. (Liskov substitution principle) • 对于面向对象的计算来说 • 子类型对象须能接收所有父类型对象能接收的消息 • 当然这个问题不总是那么简单。 Institute of Computer Software Nanjing University

  24. 继承与类型 • Are Cows Animals? -By David L. Shang class Animal proc eat (food: AnyFood); end; ---------------------------------------------------------- class Cow is Animal proc eat (food: PlantFood); end; Institute of Computer Software Nanjing University

  25. 多态与动态绑定 • 通过类继承机制实现子类型,允许子类重定义父类的操作。 • 这样父类型变量(对象引用)及其上的的操作必然是多态的。 • 这就在实现上要求动态绑定机制

  26. The dangers of static binding createa.make (…) S1 • For every creation procedure cp: {precp} docp {postcp and INV} • For every exported routine r: {INV and prer} dor {INV and postr} • The worst possible erroneous run-time situation in object-oriented software development: • Producing an object that does not satisfy the invariant of its class. a.f (…) S2 a.g (…) S3 a.f (…) S4

  27. The dangers of static binding (cont’d) • {INVA} dorA {INVA} • {INVB} dorB {INVB} • Consider a call of the form a1.r where a1 is polymorphic: • No guarantee on the effect of dorA on an instance of B! rA A rB++ B

  28. 于是 • 考虑 o.m() • 静态类型检查确保o所代表的对象可以接收消息m (至少可找到一个方法m) • 动态绑定找到“那一个”最合适的m

  29. 以下PPT直接引自东南大学陈林老师的主页下的 http://cse.seu.edu.cn/people/lchen/resource/ 该处有对Sebesta原英文ppt的中文翻译。 感谢翻译整理者。 对面向对象程序设计的支持

  30. 目录 • 引言 • 面向对象程序设计 • 面向对象语言的设计问题 • Smalltalk对OOP的支持 • C++对OOP的支持 • Java对OOP的支持 • C#对OOP的支持 • Ada 95对OOP的支持 • JavaScript的对象模型 • 面向对象构造的实现

  31. 引言 • OOPL • 有些支持过程式和面向数据的程序设计,如Ada和C++ • 有些支持函数式程序设计 • 有些新的语言一般不支持其他范型(paradigm),但可以使用命令式结构,如Java、C# • 有些是纯的OOPL,如Smalltalk

  32. 面向对象程序设计 • 抽象数据类型(封装) • 继承 • 继承是OOP和OOPL的中心议题 • 多态

  33. 继承 • 重用提供了生产力的提升 • ADT难于重用 • 所有的ADT是独立的,且处于相同层次 • 继承允许在现存类基础上定义新的类,从而继承公共部分 • 继承涉及了上述两个问题:通过少量修改重用ADT;以层次结构的方式定义类

  34. 面向对象概念 • 在OOL中,ADT称为类 • 类实例称为对象 • derived class/subclass(派生类/子类) • parent class/superclass(父类/超类) • 定义操作的子程序称为方法

  35. 面向对象概念(续) • 对方法的调用称为消息传递 • 某个对象的所有方法集合称为消息协议或消息接口 • 消息有两个部分:方法名、目标对象 • 最简单的情况是:一个类继承其父类的所有实体

  36. 面向对象概念(续) • 继承关系在对封装实体使用访问控制时变得更复杂 • 类可以对其子类隐藏所有实体 • 类可以对其客户隐藏所有实体 • 类可以对客户隐藏所有实体,而对子类开放 • 除了直接继承方法,类也可以调整继承的方法 • Override(方法覆盖)

  37. 面向对象概念(续) • 类中两种类型的变量 • 类变量 • 实例变量 • 类中两种方法 • 类方法 • 实例方法 • 单继承 vs. 多继承 • 用继承实现重用的一个缺点 • 在类间产生了交互依赖,提高了维护的复杂度

  38. 动态绑定 • 多态变量:用某个类定义一个变量,该变量能引用该类对象及其任意子类的对象 • 当类层次中包含覆盖方法,且通过多态变量调用,对方法的正确绑定是动态的 • 使软件系统在开发和维护中都更易于扩展

  39. 动态绑定概念 • 抽象方法:不包含定义的方法(仅定义了协议) • 抽象类:至少包含一个虚方法 • 抽象类不能实例化

  40. OOPL的设计问题 • 纯对象? • 子类 vs. 子类型? • 类型检查和多态? • 单继承 vs. 多继承? • 对象分配和释放? • 动态绑定 vs. 静态绑定? • 嵌套类?

  41. 纯对象 • Everything is an object • 优点:优雅纯粹 • 缺点:对单个对象操作的效率低 • 在一个完整的命令式类型系统中增加对象 • 优点:简单对象操作效率高 • 缺点:类型系统的不一致性 • 基本数据类型使用命令式语言的类型系统,而其他都是对象 • 优点:提高简单对象操作效率;类型系统更简单 • 缺点:类型系统的不一致性

  42. 子类 vs. 子类型 • 父类对象和子类对象是一种“is-a”关系? • 如果子类is-a父类,则子类对象的行为必须和父类对象的行为完全一致 • 如果子类和父类存在”is-a”关系,则子类是一个子类型 • 子类仅能以“兼容”的方式增加变量、方法和覆盖继承的方法(所谓兼容:不能改变行为)

  43. 类型检查和多态 • 多态可能需要对参数和返回值的动态类型检查 • 动态类型检查开销大,且将错误侦测延迟至运行时 • 如果方法覆盖限制为必须有相同的参数类型和返回类型,则可以使用静态类型检查

  44. 单继承 vs. 多继承 • 多继承允许一个类继承自多个类 • 多继承的缺点 • 语言实现复杂(名冲突) • 潜在的低效率:动态绑定开销变大 • 优点: • 带来某些便利

  45. 对象分配和释放 • 对象从哪个空间分配? • 如果和ADT一样,则可以从任意地方分配 • 从运行时栈分配 • 显式的从堆分配 • 如果都是堆动态的,可以统一使用引用或指针变量 • 简化赋值:解引用可以是隐式的 • 如果对象是栈动态的,对于子类型存在一个问题 • 释放是显式的还是隐式的?

  46. 动态绑定 vs. 静态绑定 • 消息的所有绑定都是动态的? • 如果全是静态的,则丧失了动态绑定的优点 • 如果全是动态的,效率低 • 允许用户指定

  47. 嵌套类 • 如果某个类只对一个一个类可见,则没有必要将其定义为对其他类也可见 • 将其嵌套在使用它的类中 • 某些情况下,类嵌套在子程序中比嵌套在另一个类中更好

  48. Smalltalk对OOP的支持 • Smalltalk是一个纯的OOPL • Everything is an object • 所有对象拥有自己的局部内存空间 • 所有计算都通过消息传递 • 没有命令式语言的外观 • 所有对象从堆中分配 • 所有对象释放是隐式的

  49. Smalltalk对OOP的支持(续) • 类型检查和多态 • 所有消息绑定都是动态的 • 消息绑定的过程:搜索消息接受的对象;没找到,则搜索父类,直到系统定义的类(没有父类) • 类型检查是动态的,仅当接受消息的对象没有匹配方法时出现类型错误

  50. Smalltalk对OOP的支持(续) • 继承 • 子类继承了所有实例变量、实例方法和类方法 • 所有子类都是子类型 • 所有继承都是实现继承( implementation inheritance ,与接口继承的区别interface inheritance ) • 没有多继承

More Related