《第十二章多态操作Polymorphic和一般类型.ppt》由会员分享,可在线阅读,更多相关《第十二章多态操作Polymorphic和一般类型.ppt(32页珍藏版)》请在三一办公上搜索。
1、第十二章 多态操作Polymorphic和一般类型,多态操作需求的引入:在数据库概念上,有许多操作,例如查询操作,需要一个通用的独立于类型的格式,从而增加了软件设计的灵活性和可复用性多态操作的分类:-特定的多态:即是普通的重载操作-包含多态:允许一个特定类型的操作可以作用载子类型对象上-有界多态:多态的一般化描述,一个操作可以定 义在多种类型上,多态引入的需要实例,示例1.对类型“人”,有年龄age和增加年龄 IncAgeP的操作;对类型“酒”,有酒龄age和酒龄的增加 操作IncAgeWType Person is type wine is name:string;name:string;a
2、ge:int;age:int;spouse:Person;bouquet:string;,declare increaseAge:Person|int-PersonDeclare 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 length:CylinderList|-int cod
3、e 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 lengthW;,有界多态需要的新概念,类型变量 type variables
4、类型表达式 type expressions类型界限 type boundaries多态操作说明 Polymorphic operation declaration,类型变量,引入目的:类型变量可以提供必要的抽象机制来隐藏不同类型的定义和操作说明.作用:在多态操作说明时,用一个类型变量来表达操作参数的抽象类型,只有在该操作被引用激活时,这个类型变量才被替换类型变量的说明形式:在特殊符号“”后面随一个普通的标识符例:Agetype;Listtype,示例1定义了一个通用的增加年龄的操作:polymorph declare increaseAge:AgeType|-AgeType code pol
5、yIncAge;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;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也是类型表达式4.如果e是类型表达式,则也是
7、类型表达式5如果e是类型表达式,则e也是类型表达式,示例,age:intage:AttrTypeElementTypeA:Elementtype;,类型界限定义,如果TV是一个类型变量,e是一个类型表达式,则TV=e是一个类型界限示例1:AgeType=age:int 语义:在类型替换时,只有具有一个名 字为age的属性,且该属性类型为int的 对象类型才能被替换,示例2:ListType 语义:替换ListType的类型必须是一个 表结构类型且它的表元素类型为 ElementType,而元素类型 Elementtype不在进一步被约束(什 么类型都可以),多态操作说明,示例1.对增加年龄的多
8、态操作的说明 多态说明: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 polyLength is var item:ElementType;I:int:=0;Begin foreach(item in self)I:=I+1;return
9、 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;code swanWedding;,implemen
10、tation 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;end define swanWedding;End type person end type Swan,生物LivingBeing的类型层次图,LivingBeing,Person,Swan,Stud
11、ent,Employee,Manager,结婚操作的多态定义,特殊的约束条件:任何生物的配偶必须是 自己的同类,其约束表达式为:WeddingTypevoid code polyMarry;,结婚引用示例,Var john:Employee;Paul,Maria:Person;Swen,Swenja:Swan;Paul.marry(Maria);!okayJohn.marry(Maria);!okayMaria.marry(John);!okayMaria.marry(Paul);!okay,(5)Swen.marry(Swenja);!okay(6)Maria.marry(Swen);!il
12、legal(7)Swen.marry(Maria);!illegal(8)Paul.marry(Paul);!okay-as far as typing is!concerned,作为操作参数的多形操作定义,一个通用的操作仍然可以作为另外操作的参数被说明,相当于在C中函数名可以作为一个参数一样示例:在一个含有多种几何对象的几何体集合中,需要将其 邻平均 一个共同的距离 该操作translateSet需要满足以下条件:-操作的接受类型是一个集合类型-集合中所有元素都能进行translate操作,先定义操作translate:translate:vertex-void再定义类型界限:TraSetv
13、oid;语义:-Traset类型变量是一个含有 TraElem元素的集合-TraElem元素类型是一个具有 translate操作的类型,具体的translateset的定义,Polymorph declare translateSet(TraSetvoid;);TraSet|Vertex-voidcode translateSetCode;Define translateSetCode(t)isVar geo:TraElem;begin foreach(geo in self)geo.translate(t);end define translateSetCode;,具体的应用举例,Var
14、myCylinderSet:CylinderSet myCuboidSet:CuboidSet;translationVector:Vertex;myCylinderSet.translateSet(translationVector);myCuboidSet.translateSet(translationVector);,内嵌的多形操作举例,对于一个可供二次开发的面向对象的系统软件而言,应当提供一系列的公用的多形操作,以供二次开发人员使用,以减少重复编码,提高开发周期一些通用的多形操作说明举例polymorph declare first(ListType):ListType|ElemTy
15、pe;polymorph declare rest(ListType):ListType|ListType;polymorph declare append(ListType):ListType|ListType ListType;polymorph declare insert(SetType ElemType)SetType|ElemType SetType;polymorph declare delete(SetType ElemType)SetType|ElemType SetType;polymorph declare delete(SetType ElemType)SetType|
16、SetType SetType;,一般类型(类属类),可以将多形的概念引入类型定义上在抽取不同类型间的共同特征的基础上,定义一个一般的类型生成框-一般类型一般类型并不能实例化任何一个对象实例对一般类型施加的操作只有初始化,它产生一个具体的类型,对它不能施加转换操作和监测操作构造一般类型时,通常利用类型界限对它所能够产生的类型施加约束,一个一般类型的实例-STOCK,在不同的管理系统中,应用着形形色色的仓库如零件库,酒窖不同的仓库有共性的特征:入库,出库,盘点,计算总价值实现仓库管理的办法之一-构造各自不同的仓库类型问题:-大量重复的代码编写-仓库管理需要增加新功能时,又要分别进行扩充实现方式之
17、二 构造一个抽象的仓库模型(一般类型),并利用初始化操作初始化出各自需要的具体仓库仓库的一般类型STOCK施加的约束是:仓库中存放的元素类型必须是元组类型,且有编号和价值两个属性,即ItemType=itemNo:int;value:float;,示例,两个具体的需要仓库管理功能的类型CuboidStock,WineStockpresisitent type CuboidItem supertype Cuboid is itemNo:int;persistent type CuboidStock supertype ANY is public store,get,totalValuebody
18、CuboidItem operations declare store:CuboidItem-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
19、:-float;,一般的仓库管理类型STOCK,persistent generic type STOCK(ItemTypeitemNo:int;value:float;)ispublic store,get,totalValuebody ItemTypeoperationsdeclare store:ItemType void;declare get:int ItemType;declare totalValue:float;end generic type STOCKpersistent type CuboidStock is STOCK(CuboidItem);persistent type WineStock is STOCK(WineItem),