多态操作Polymorphic和一般类型ppt课件_第1页
多态操作Polymorphic和一般类型ppt课件_第2页
多态操作Polymorphic和一般类型ppt课件_第3页
多态操作Polymorphic和一般类型ppt课件_第4页
多态操作Polymorphic和一般类型ppt课件_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

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

文档简介

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

2、g; age:int; age:int; spouse: Person; bouquet:string;declare increaseAge:Person|int-PersonDeclare increaseAge:Wine|int -Wine1define incAgePn is self.age:=self.age+n;2define incAgeWn is self.age:=self.age+n; 例如2:不同表构造类型的长度操作定义 Type CylinderList is type WineList is ; ; 1declare length:CylinderList|-int

3、 code lengthC; 2declare length:WineList|-int code lengthW;1define lengthC is 2 define lengthW is var cyl:Cylinder; var wine:Wine; I:int:=0; I:int:=0;Begin begin foreachcyl in self foreachwine in self I:=I+1; I:=I+1; return I; return I;End define lengthC; end define lengthW;有界多态需求的新概念 类型变量 type varia

4、bles 类型表达式 type expressions 类型界限 type boundaries 多态操作阐明 Polymorphic operation declaration类型变量 引入目的:类型变量可以提供必要的笼统机制来隐藏不同类型的定义和操作阐明. 作用:在多态操作阐明时,用一个类型变量来表达操作参数的笼统类型,只需在该操作被援用激活时,这个类型变量才被交换 类型变量的阐明方式: 在特殊符号“后面随一个普通的标识符例:Agetype ;Listtype 例如1定义了一个通用的添加年龄的操作: polymorph declare increaseAge:AgeType| -AgeTy

5、pe code polyIncAge; and the unmodified definition define polyIncAgen is self.age:=self.age+n; 例如2: 定义一个通用的求表长度的操作: Polymorph declare length:ListType|-int code polyLength; Define polyLength is var item :ElementType; I:int:=0; begin foreachitem in self I:=I+1; End define polyLength;产生的问题分析 需求一个对类型变量的约

6、束机制,以控制被代真交换的类型都是合法类型 对例1:可以交换Agetype 的类型必需有一 个int 类型的属性Age; 对例2:可以交换ListType的类型只能是表 构造类型 对例2:假设一个元素为t类型的表构造 被代入ListType;那么只需t可以交换 ListType 类型变量的约束机制称为类型界限 类型界限需求用类型表达式来描画 类型表达式允许有三种表达方式: -元组表达:A1:T1,An:Tn -集合表达:T -表 表达:类型表达式定义1.每个类型符号是一个类型表达式2.每个类型变量是一个类型表达式3.假设A1,An是属性符号,e1en是类型表达式,那么A1:e1,An:en也是

7、类型表达式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的类型必需是一个 表构造类型且它的表元素类型为 ElementType,而元素类型 Elementtype不在进一步被约束什 么

8、类型都可以多态操作阐明例如1.对添加年龄的多态操作的阐明 多态阐明:polymorph declare increaseAgeAgeType AgeType code polyAge; 不变的操作定义:define polyIncAgen is self.age:=self.age+n;例如2.对表构造类型长度计算的多态操作 polymorph declare lengthListType=:ListType|-int code polyLength; define polyLength is var item:ElementType; I:int:=0;Begin foreach item

9、in self I:=I+1; return I;End define polyLength; 一个例如Persistent type person persistent type Swansupertype LivingBeing is supertype Livingbeing isPublic marry, public marry,Bodyspouse:person; bodyspouse:Swan; ; ;Operations operations declare declare marry:person-void marry:Swan-void code personWedding

10、; code swanWedding; implementation implementation define define personWeddingvictim is swanWeddingvictimis begin begin self.spouse:=victim; self.spouse:=victim; victim.spouse:=self; victim.spouse:=self; end define personWedding; end define swanWedding; End type person end type Swan 生物LivingBeing的类型层

11、次图LivingBeingPersonSwanStudentEmployeeManager结婚操作的多态定义 特殊的约束条件:任何生物的配偶必需是 本人的同类,其约束表达式为: WeddingType=spouse:WeddingType Marry的多态操作声明: Polymorph declare marryWeddingTypevoid code polyMarry; 结婚援用例如Var john:Employee; Paul,Maria:Person; Swen,Swenja:Swan;Paul.marryMaria;!okayJohn.marryMaria;!okayMaria.ma

12、rryJohn;!okayMaria.marryPaul;!okay5Swen.marrySwenja;!okay6Maria.marrySwen;!illegal7Swen.marryMaria;!illegal8Paul.marryPaul;!okay-as far as typing is ! concerned作为操作参数的多形操作定义 一个通用的操作依然可以作为另外操作的参数被阐明,相当于在C中函数名可以作为一个参数一样 例如:在一个含有多种几何对象的几何体集合中,需求将其 邻平均 一个共同的间隔 该操作translateSet需求满足以下条件: -操作的接受类型是一个集合类型 -集

13、合中一切元素都能进展translate操作 先定义操作translate:translate:vertex-void 再定义类型界限: TraSet=TraElem; TraElemvoid; 语义:-Traset类型变量是一个含有 TraElem元素的集合 -TraElem元素类型是一个具有 translate操作的类型 详细的translateset的定义Polymorph declare translateSetTraSet=TraElem,TraElemvoid;TraSet|Vertex-voidcode translateSetCode;Define translateSetCod

14、et isVar geo:TraElem; begin foreachgeo in self geo.translatet; end define translateSetCode;详细的运用举例Var myCylinderSet:CylinderSet myCuboidSet:CuboidSet; translationVector:Vertex;myCylinderSet.translateSettranslationVector;myCuboidSet.translateSettranslationVector;内嵌的多形操作举例 对于一个可供二次开发的面向对象的系统软件而言,该当提供一

15、系列的公用的多形操作,以供二次开发人员运用,以减少反复编码,提高开发周期 一些通用的多形操作阐明举例 polymorph declare first ListType: ListType | ElemType; polymorph declare rest ListType:ListType | ListType; polymorph declare append ListType:ListType | ListType ListType; polymorph declare insert SetType ElemTypeSetType | ElemType SetType; polymorp

16、h declare delete SetType ElemTypeSetType | ElemType SetType; polymorph declare delete SetType ElemTypeSetType | SetType SetType;普通类型类属类 可以将多形的概念引入类型定义上 在抽取不同类型间的共同特征的根底上,定义一个普通的类型生成框-普通类型 普通类型并不能实例化任何一个对象实例 对普通类型施加的操作只需初始化,它产生一个详细的类型,对它不能施加转换操作和监测操作 构造普通类型时,通常利用类型界限对它所可以产生的类型施加约束一个普通类型的实例-STOCK 在不同的

17、管理系统中,运用着形形色色的仓库如零件库,酒窖 不同的仓库有共性的特征:入库,出库,清点,计算总价值 实现仓库管理的方法之一-构造各自不同的仓库类型 问题:-大量反复的代码编写 -仓库管理需求添加新功能时,又要分别进展扩展 实现方式之二 构造一个笼统的仓库模型普通类型,并利用初始化操作初始化出各自需求的详细仓库 仓库的普通类型STOCK施加的约束是:仓库中存放的元素类型必需是元组类型,且有编号和价值两个属性,即ItemType void; declare totalValue: - float; persistent type Wine is name: 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

温馨提示

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

评论

0/150

提交评论