一、类的声明
类的声明由简单地三部分组成,就是关键字Class、类的名称、类的特性(property)表:
Class } |
同时,在ODL中,类的属性、方法和类之间的联系称为类的特性
二、属性
属性声明也是由三部分组成,即关键字attribute、类型和属性名称:
attribute
[例子1]定义一个学生类和课程类:
Class Student{ attribute string Sname; attributestring Ssex; attribute integer Sage; attribute string Sdept; } |
Class Course{ attribute string Cname; attribute string Cpno; attribute integer Ccredit; } |
string和integer是原子类型。面向对象数据模型还允许使用构造类型,例如,给学生类增加一个PhoneList属性,用于存放学生的电话号码,可以用string存储电话号码。
attribute set
三、方法
方法有方法名字、输入参数、输出参数、返回值和抛出的异常。类的方法可以由类的所有对象调用。方法至少要有一个输入参数,它是类的对象,这个参数是隐含的,方法的调用者就是这个参数。
同一个函数名可以作为不同的类的方法,由于有隐含的输入参数,所以,虽然方法的名字相同,但是方法却是不同的方法,这样就达到了重载(overload)
的目的。
Class Student{ attribute string Sname; attribute string Ssex; atribute integer Sage; attribute string Sdept; attribute Iset Winterger ShowName(out string); } |
四、联系
一个学生可以选修多门课程,一个课程有多个学生选修,学生类和课程类之间存在一个多对多联系。为了表达一个学生可以选修多门课程,在Student类中增加1行:
relationship set
Course是被引用的类,set是集合类型,表示Student类的对象可以引用Course类的一组对象。
同样,在Course类中也要增加1行:
relationship set
Courses和Students是同一个联系的2个方向,为了说明这种关系,在relationship中增加关键字inverse加以说明:
relationship set
relationship set
完整的Student类和Course类.
Student类 | Course类 |
Class Student{ |
Class Course{ attribute string Cname; attribute string Cpno; attribute integer Ccredit; Relationship set } |
五、码
ODL提供了说明码的方法:
Class
关键字key和keys是同义词,keylist是码表,每个码由类中的一个或多个属性构成。
Class Student(key Sno){
....
}
六、子类(subclass)
ODL支持单继承和多继承。研究生类是学生的子类,它继承了学生类的所有特性,还有自己的导师属性。
Class Postgraduate extends Student{
attribute string Supervisor;
}
如果类C是类C1、C2、...、Ca的子类:
Class
}
七、外延(extent)
外延是类的对象的集合.面向对象数据库使用外延存储对象,实现对象的持久化.一般情况下,类的名字是单数名词,而外延是双数名词.
ODL的语法为
Class
}
Student类的完整说明,包括码和外延 |
Class Student(extent Students key Sno){ attribute string Sname; attribute string Ssex; atribute integer Sage; attribute string Sdept; relationship set interger ShowName(out string); } |
八、类型(type)
ODL的基础类型有:
原子类型:如integer、float、character、character string、boolean和enumerations。
类 名:如Student、Course。它实际上是一个结构类型,包括所有的属性和联系。
ODL常用的构造器有:
·Set:T是任意类型,Set
·Bag:同Set,只是允许出现相同的元素。
·List:T是任意类型,List
·Array:T是任意类型,i是任意整数,Array
·Dictionary:T和S是任意类型,Dictionary