第十二章多态操作Polymorphic和一般类型.ppt

上传人:本田雅阁 文档编号:3168357 上传时间:2019-07-19 格式:PPT 页数:32 大小:137.52KB
返回 下载 相关 举报
第十二章多态操作Polymorphic和一般类型.ppt_第1页
第1页 / 共32页
第十二章多态操作Polymorphic和一般类型.ppt_第2页
第2页 / 共32页
第十二章多态操作Polymorphic和一般类型.ppt_第3页
第3页 / 共32页
第十二章多态操作Polymorphic和一般类型.ppt_第4页
第4页 / 共32页
第十二章多态操作Polymorphic和一般类型.ppt_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《第十二章多态操作Polymorphic和一般类型.ppt》由会员分享,可在线阅读,更多相关《第十二章多态操作Polymorphic和一般类型.ppt(32页珍藏版)》请在三一文库上搜索。

1、第十二章 多态操作Polymorphic和一般类型,多态操作需求的引入:在数据库概念上,有许多操作,例如查询操作,需要一个通用的独立于类型的格式,从而增加了软件设计的灵活性和可复用性 多态操作的分类: -特定的多态:即是普通的重载操作 -包含多态:允许一个特定类型的操作可以作用载子类型对象上 -有界多态:多态的一般化描述,一个操作可以定 义在多种类型上,多态引入的需要实例,示例1.对类型“人”,有年龄age和增加年龄 IncAgeP的操作; 对类型“酒”,有酒龄age和酒龄的增加 操作IncAgeW Type Person is type wine is name: string; name:

2、string; age:int; age:int; spouse: Person; bouquet:string;,declare increaseAge:Person|int-Person Declare increaseAge:Wine|int -Wine (1)define incAgeP(n) is self.age:=self.age+n; (2)define incAgeW(n) is self.age:=self.age+n;,示例2:不同表结构类型的长度操作定义,Type CylinderList is type WineList is ; ; (1)declare lengt

3、h:CylinderList|-int code lengthC; (2)declare length:WineList|-int code lengthW;,(1)define lengthC is (2) define lengthW is var cyl:Cylinder; var wine:Wine; I:int:=0; I:int:=0; Begin begin foreach(cyl in self) foreach(wine in self ) I:=I+1; I:=I+1; return I; return I; End define lengthC; end define l

4、engthW;,有界多态需要的新概念,类型变量 type variables 类型表达式 type expressions 类型界限 type boundaries 多态操作说明 Polymorphic operation declaration,类型变量,引入目的:类型变量可以提供必要的抽象机制来隐藏不同类型的定义和操作说明. 作用:在多态操作说明时,用一个类型变量来表达操作参数的抽象类型,只有在该操作被引用激活时,这个类型变量才被替换 类型变量的说明形式: 在特殊符号“”后面随一个普通的标识符例:Agetype ;Listtype,示例1定义了一个通用的增加年龄的操作: polymorph

5、 declare increaseAge:AgeType| -AgeType code polyIncAge; and the unmodified definition define polyIncAge(n) is self.age:=self.age+n;,示例2: 定义一个通用的求表长度的操作: Polymorph declare length:ListType|-int code polyLength; Define polyLength is var item :ElementType; I:int:=0; begin foreach(item in self) I:=I+1; E

6、nd define polyLength;,产生的问题分析,需要一个对类型变量的约束机制,以控制被代真(替换)的类型都是合法类型 对例1:能够替换Agetype 的类型必须有一 个int 类型的属性Age; 对例2:能够替换ListType的类型只能是表 结构类型 对例2:如果一个元素为t类型的表结构 被代入ListType;那么只有t可以替换 ListType,类型变量的约束机制称为类型界限 类型界限需要用类型表达式来描述 类型表达式允许有三种表达方式: -元组表达:A1:T1,An:Tn -集合表达:T -表 表达:,类型表达式定义,1.每个类型符号是一个类型表达式 2.每个类型变量是一个

7、类型表达式 3.如果A1,An是属性符号,e1en是类型表达式,那么A1:e1,An:en也是类型表达式 4.如果e是类型表达式,则也是类型表达式 5如果e是类型表达式,则e也是类型表达式,示例,age:int age:AttrType ElementType A:Elementtype;,类型界限定义,如果TV是一个类型变量,e是一个类型表达式,则TV=e是一个类型界限 示例1:AgeType=age:int 语义:在类型替换时,只有具有一个名 字为age的属性,且该属性类型为int的 对象类型才能被替换,示例2:ListType 语义:替换ListType的类型必须是一个 表结构类型且它的

8、表元素类型为 ElementType,而元素类型 Elementtype不在进一步被约束(什 么类型都可以),多态操作说明,示例1.对增加年龄的多态操作的说明 多态说明:polymorph declare increaseAge(AgeType AgeType code polyAge; 不变的操作定义:define polyIncAge(n) is self.age:=self.age+n;,示例2.对表结构类型长度计算的多态操作 polymorph declare length(ListType):ListType|-int code polyLength; define polyLeng

9、th is var item:ElementType; I:int:=0; Begin foreach (item in self) I:=I+1; return I; End define polyLength;,一个示例,Persistent type person persistent type Swan supertype LivingBeing is supertype Livingbeing is Public marry, public marry, Bodyspouse:person; bodyspouse:Swan; ; ; Operations operations dec

10、lare declare marry:person-void marry:Swan-void code personWedding; code swanWedding; ,implementation implementation define define personWedding(victim) is swanWedding(victim)is begin begin self.spouse:=victim; self.spouse:=victim; victim.spouse:=self; victim.spouse:=self; end define personWedding; e

11、nd define swanWedding; End type person end type Swan,生物LivingBeing的类型层次图,LivingBeing,Person,Swan,Student,Employee,Manager,结婚操作的多态定义,特殊的约束条件:任何生物的配偶必须是 自己的同类,其约束表达式为: WeddingTypevoid code polyMarry;,结婚引用示例,Var john:Employee; Paul,Maria:Person; Swen,Swenja:Swan; Paul.marry(Maria);!okay John.marry(Mari

12、a);!okay Maria.marry(John);!okay Maria.marry(Paul);!okay,(5)Swen.marry(Swenja);!okay (6)Maria.marry(Swen);!illegal (7)Swen.marry(Maria);!illegal (8)Paul.marry(Paul);!okay-as far as typing is ! concerned,作为操作参数的多形操作定义,一个通用的操作仍然可以作为另外操作的参数被说明,相当于在C中函数名可以作为一个参数一样 示例:在一个含有多种几何对象的几何体集合中,需要将其 邻平均 一个共同的距离

13、该操作translateSet需要满足以下条件: -操作的接受类型是一个集合类型 -集合中所有元素都能进行translate操作,先定义操作translate:translate:vertex-void 再定义类型界限: TraSetvoid; 语义:-Traset类型变量是一个含有 TraElem元素的集合 -TraElem元素类型是一个具有 translate操作的类型,具体的translateset的定义,Polymorph declare translateSet (TraSetvoid;); TraSet|Vertex-void code translateSetCode; Defi

14、ne translateSetCode(t) is Var geo:TraElem; begin foreach(geo in self) geo.translate(t); end define translateSetCode;,具体的应用举例,Var myCylinderSet:CylinderSet myCuboidSet:CuboidSet; translationVector:Vertex; myCylinderSet.translateSet(translationVector); myCuboidSet.translateSet(translationVector);,内嵌的多

15、形操作举例,对于一个可供二次开发的面向对象的系统软件而言,应当提供一系列的公用的多形操作,以供二次开发人员使用,以减少重复编码,提高开发周期 一些通用的多形操作说明举例 polymorph declare first (ListType): ListType | ElemType; polymorph declare rest (ListType): ListType | ListType; polymorph declare append (ListType): ListType | ListType ListType; polymorph declare insert (SetType E

16、lemType)SetType | ElemType SetType; polymorph declare delete (SetType ElemType) SetType | ElemType SetType; polymorph declare delete (SetType ElemType) SetType | SetType SetType;,一般类型(类属类),可以将多形的概念引入类型定义上 在抽取不同类型间的共同特征的基础上,定义一个一般的类型生成框-一般类型 一般类型并不能实例化任何一个对象实例 对一般类型施加的操作只有初始化,它产生一个具体的类型,对它不能施加转换操作和监测

17、操作 构造一般类型时,通常利用类型界限对它所能够产生的类型施加约束,一个一般类型的实例-STOCK,在不同的管理系统中,应用着形形色色的仓库如零件库,酒窖 不同的仓库有共性的特征:入库,出库,盘点,计算总价值 实现仓库管理的办法之一-构造各自不同的仓库类型 问题:-大量重复的代码编写 -仓库管理需要增加新功能时,又要分别进行扩充 实现方式之二 构造一个抽象的仓库模型(一般类型),并利用初始化操作初始化出各自需要的具体仓库 仓库的一般类型STOCK施加的约束是:仓库中存放的元素类型必须是元组类型,且有编号和价值两个属性,即 ItemType = itemNo: int ; value: floa

18、t;,示例,两个具体的需要仓库管理功能的类型CuboidStock,WineStock presisitent type CuboidItem supertype Cuboid is itemNo: int; persistent type CuboidStock supertype ANY is public store, get, totalValue body CuboidItem operations declare store: CuboidItem - void; declare totalValue: - float; persistent type Wine is name:

19、string; age: int; value:float; persistent type WineStock supertype ANY is public store, get, totalValue body WineItem operations declare store: WineItem - void; declare get: it - WineItem; declare totalValue: - float;,一般的仓库管理类型STOCK,persistent generic type STOCK(ItemTypeitemNo: int; value: float;) is public store, get, totalValue body ItemType operations declare store: ItemType void; declare get: int ItemType; declare totalValue: float; end generic type STOCK persistent type CuboidStock is STOCK(CuboidItem); persistent type WineStock is STOCK(WineItem),

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1