1 age Inc Age P age Inc Age

  • Slides: 32
Download presentation

多态引入的需要实例 示例1. 对类型“人”, 有年龄age和增加年龄 Inc. Age. P的操作; 对类型“酒”, 有酒龄age和酒龄的增加 操作Inc. Age. W Type Person

多态引入的需要实例 示例1. 对类型“人”, 有年龄age和增加年龄 Inc. Age. P的操作; 对类型“酒”, 有酒龄age和酒龄的增加 操作Inc. Age. W Type Person is type wine is [name: string; age: int; spouse: Person; ]; bouquet: string; ];

(1) declare increase. Age: Person||int->Person (2) Declare increase. Age: Wine||int ->Wine (1)define inc. Age.

(1) declare increase. Age: Person||int->Person (2) Declare increase. Age: Wine||int ->Wine (1)define inc. Age. P(n) is self. age: =self. age+n; (2)define inc. Age. W(n) is self. age: =self. age+n;

示例2: 不同表结构类型的长度 操作定义 • Type Cylinder. List is type Wine. List is <Cylinder>; <Wine>;

示例2: 不同表结构类型的长度 操作定义 • Type Cylinder. List is type Wine. List is <Cylinder>; <Wine>; (1)declare length: Cylinder. List||->int code length. C; (2)declare length: Wine. List||->int code length. W;

(1)define length. C is var cyl: Cylinder; I: int: =0; Begin foreach(cyl in self)

(1)define length. C is var cyl: Cylinder; I: int: =0; Begin foreach(cyl in self) I: =I+1; return I; End define length. C; (2) define length. W is var wine: Wine; I: int: =0; begin foreach(wine in self ) I: =I+1; return I; end define length. W;

有界多态需要的新概念 • • 类型变量 type variables 类型表达式 type expressions 类型界限 type boundaries 多态操作说明 Polymorphic

有界多态需要的新概念 • • 类型变量 type variables 类型表达式 type expressions 类型界限 type boundaries 多态操作说明 Polymorphic operation declaration

 • 示例1定义了一个通用的增加年龄的操作: polymorph declare increase. Age: Age. Type|| ->Age. Type code poly. Inc.

• 示例1定义了一个通用的增加年龄的操作: polymorph declare increase. Age: Age. Type|| ->Age. Type code poly. Inc. Age; and the unmodified definition define poly. Inc. Age(n) is self. age: =self. age+n;

 • 示例2: 定义一个通用的求表长度的操作: Polymorph declare length: List. Type||->int code poly. Length; Define poly.

• 示例2: 定义一个通用的求表长度的操作: Polymorph declare length: List. Type||->int code poly. Length; Define poly. Length is var item : Element. Type; I: int: =0; begin foreach(item in self) I: =I+1; End define poly. Length;

示例 • • • [age: int] [age: Attr. Type] <Element. Type> {Element. Type} [A:

示例 • • • [age: int] [age: Attr. Type] <Element. Type> {Element. Type} [A: Elementtype>; ]

多态操作说明 示例1. 对增加年龄的多态操作的说明 多态说明: polymorph declare increase. Age(Age. Type<=[age: int; ]): Age. Type ||int

多态操作说明 示例1. 对增加年龄的多态操作的说明 多态说明: polymorph declare increase. Age(Age. Type<=[age: int; ]): Age. Type ||int -> Age. Type code poly. Age; 不变的操作定义: define poly. Inc. Age(n) is self. age: =self. age+n;

示例2. 对表结构类型长度计算的多态操作 polymorph declare length(List. Type<=<Element. Type>): List. Type||>int code poly. Length; define poly.

示例2. 对表结构类型长度计算的多态操作 polymorph declare length(List. Type<=<Element. Type>): List. Type||>int code poly. Length; define poly. Length is var item: Element. Type; I: int: =0; Begin foreach (item in self) I: =I+1; return I; End define poly. Length;

一个示例 Persistent type person persistent type Swan supertype Living. Being is supertype Livingbeing is

一个示例 Persistent type person persistent type Swan supertype Living. Being is supertype Livingbeing is Public marry, … public marry, … Body[spouse: person; body[spouse: Swan; …]; Operations operations declare marry: person->void marry: Swan->void code person. Wedding; code swan. Wedding; … …

implementation define person. Wedding(victim) is begin self. spouse: =victim; victim. spouse: =self; implementation define

implementation define person. Wedding(victim) is begin self. spouse: =victim; victim. spouse: =self; implementation define swan. Wedding(victim)is begin self. spouse: =victim; victim. spouse: =self; end define person. Wedding; end define swan. Wedding; … … End type person end type Swan

生物Living. Being的类型层次图 Living. Being Swan Person Student Employee Manager

生物Living. Being的类型层次图 Living. Being Swan Person Student Employee Manager

结婚操作的多态定义 • 特殊的约束条件: 任何生物的配偶必须是 自己的同类, 其约束表达式为: Wedding. Type<=[spouse: Wedding. Type] • Marry的多态操作声明: Polymorph declare

结婚操作的多态定义 • 特殊的约束条件: 任何生物的配偶必须是 自己的同类, 其约束表达式为: Wedding. Type<=[spouse: Wedding. Type] • Marry的多态操作声明: Polymorph declare marry(Wedding. Type<= [spouse: Wedding. Type; ]): Wedding. Type|| Wedding. Type->void code poly. Marry;

结婚引用示例 Var john: Employee; Paul, Maria: Person; Swen, Swenja: Swan; … (1) Paul. marry(Maria);

结婚引用示例 Var john: Employee; Paul, Maria: Person; Swen, Swenja: Swan; … (1) Paul. marry(Maria); !!okay (2) John. marry(Maria); !!okay (3) Maria. marry(John); !!okay (4) 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

(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

 • 先定义操作translate: [translate: vertex>void] • 再定义类型界限:  Tra. Set<={Tra. Elem}; Tra. Elem<=[translate: Vertex->void;

• 先定义操作translate: [translate: vertex>void] • 再定义类型界限: Tra. Set<={Tra. Elem}; Tra. Elem<=[translate: Vertex->void; ] 语义: ---Traset类型变量是一个含有 Tra. Elem元素的集合 ---Tra. Elem元素类型是一个具有 translate操作的类型

具体的translateset的定义 Polymorph declare translate. Set (Tra. Set<={Tra. Elem}, Tra. Elem<=[translate: Vertex>void; ]); Tra. Set||Vertex->void

具体的translateset的定义 Polymorph declare translate. Set (Tra. Set<={Tra. Elem}, Tra. Elem<=[translate: Vertex>void; ]); Tra. Set||Vertex->void code translate. Set. Code; Define translate. Set. Code(t) is Var geo: Tra. Elem; begin foreach(geo in self) geo. translate(t); end define translate. Set. Code;

具体的应用举例 Var my. Cylinder. Set: Cylinder. Set my. Cuboid. Set: Cuboid. Set; translation. Vector:

具体的应用举例 Var my. Cylinder. Set: Cylinder. Set my. Cuboid. Set: Cuboid. Set; translation. Vector: Vertex; (1) my. Cylinder. Set. translate. Set(translation. Vec tor); (2) my. Cuboid. Set. translate. Set(translation. Vect or);

内嵌的多形操作举例 • 对于一个可供二次开发的面向对象的系统软件而言, 应 当提供一系列的公用的多形操作, 以供二次开发人员使 用, 以减少重复编码, 提高开发周期 • 一些通用的多形操作说明举例 – polymorph declare

内嵌的多形操作举例 • 对于一个可供二次开发的面向对象的系统软件而言, 应 当提供一系列的公用的多形操作, 以供二次开发人员使 用, 以减少重复编码, 提高开发周期 • 一些通用的多形操作说明举例 – polymorph declare first (List. Type≤<Elem. Type>): List. Type || → Elem. Type; – polymorph declare rest (List. Type≤<Elem. Type>): List. Type || → List. Type; – polymorph declare append (List. Type≤< Elem. Type>): List. Type || List. Type → List. Type; – polymorph declare insert (Set. Type≤{ Elem. Type})Set. Type || Elem. Type → Set. Type; – polymorph declare delete (Set. Type≤{ Elem. Type}) Set. Type || Set. Type → Set. Type;

示例 • 两个具体的需要仓库管理功能的类型 Cuboid. Stock, Wine. Stock – presisitent type Cuboid. Item supertype Cuboid

示例 • 两个具体的需要仓库管理功能的类型 Cuboid. Stock, Wine. Stock – presisitent type Cuboid. Item supertype Cuboid is [ item. No: int; ]; persistent type Cuboid. Stock supertype ANY is public store, get, total. Value body { Cuboid. Item} operations declare store: Cuboid. Item -> void; declare total. Value: -> float; – persistent type Wine is [ name: string; age: int; value: float; ]; persistent type Wine. Stock supertype ANY is public store, get, total. Value body { Wine. Item } operations declare store: Wine. Item -> void; declare get: it -> Wine. Item; declare total. Value: -> float;

一般的仓库管理类型STOCK – persistent generic type STOCK(Item. Type≤[item. No: int; value: float; ]) is public

一般的仓库管理类型STOCK – persistent generic type STOCK(Item. Type≤[item. No: int; value: float; ]) is public store, get, total. Value body {Item. Type} operations declare store: Item. Type void; declare get: int Item. Type; declare total. Value: float; end generic type STOCK persistent type Cuboid. Stock is STOCK(Cuboid. Item); persistent type Wine. Stock is STOCK(Wine. Item)