面向对象数据模型的构建和分析
一、类的声明
类的声明由简单地三部分组成,就是关键字Class、类的名称、类的特性(property)表:
Class <name>{ } |
同时,在ODL中,类的属性、方法和类之间的联系称为类的特性
二、属性
属性声明也是由三部分组成,即关键字attribute、类型和属性名称:
attribute <type><name>
[例子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<string> PhoneList
三、方法
方法有方法名字、输入参数、输出参数、返回值和抛出的异常。类的方法可以由类的所有对象调用。方法至少要有一个输入参数,它是类的对象,这个参数是隐含的,方法的调用者就是这个参数。
同一个函数名可以作为不同的类的方法,由于有隐含的输入参数,所以,虽然方法的名字相同,但是方法却是不同的方法,这样就达到了重载(overload)
的目的。
Class Student{ attribute string Sname; attribute string Ssex; atribute integer Sage; attribute string Sdept; attribute Iset<string>PhoneList; Winterger ShowName(out string); } |
四、联系
一个学生可以选修多门课程,一个课程有多个学生选修,学生类和课程类之间存在一个多对多联系。为了表达一个学生可以选修多门课程,在Student类中增加1行:
relationship set<Course>Courses;relationship表示后面的Courses是一个引用。
Course是被引用的类,set是集合类型,表示Student类的对象可以引用Course类的一组对象。
同样,在Course类中也要增加1行:
relationship set<Student> Students;
Courses和Students是同一个联系的2个方向,为了说明这种关系,在relationship中增加关键字inverse加以说明:
relationship set<Course>Courses inverse Course::Students;
relationship set<Student>Students inverse student:Courses;
完整的Student类和Course类.
Student类 | Course类 |
Class Student{ |
Class Course{ attribute string Cname; attribute string Cpno; attribute integer Ccredit; Relationship set<Student> Students inverse Student:Courses; } |
五、码
ODL提供了说明码的方法:
Class <name>(<key I keys> <keylist>){}
关键字key和keys是同义词,keylist是码表,每个码由类中的一个或多个属性构成。
Class Student(key Sno){
....
}
六、子类(subclass)
ODL支持单继承和多继承。研究生类是学生的子类,它继承了学生类的所有特性,还有自己的导师属性。
Class Postgraduate extends Student{
attribute string Supervisor;
}
如果类C是类C1、C2、...、Ca的子类:
Class <name>extends C1:C2....Cn{
<list of property>
}
七、外延(extent)
外延是类的对象的集合.面向对象数据库使用外延存储对象,实现对象的持久化.一般情况下,类的名字是单数名词,而外延是双数名词.
ODL的语法为
Class <name>(extent <extentName>){
<list of property>
}
Student类的完整说明,包括码和外延 |
Class Student(extent Students key Sno){ attribute string Sname; attribute string Ssex; atribute integer Sage; attribute string Sdept; relationship set<Course>Courses inverse Course::Students; interger ShowName(out string); } |
八、类型(type)
ODL的基础类型有:
原子类型:如integer、float、character、character string、boolean和enumerations。
类 名:如Student、Course。它实际上是一个结构类型,包括所有的属性和联系。
ODL常用的构造器有:
·Set:T是任意类型,Set<T>是一个类型,它表示一个集合,集合的元素是类型T的元素。
·Bag:同Set,只是允许出现相同的元素。
·List:T是任意类型,List<T>是一个类型,其值是由T的0到多个元素组成的表
·Array:T是任意类型,i是任意整数,Array<T,i>是一个类型,它表示T的i个元素构成的数组。
·Dictionary:T和S是任意类型,Dictionary<T,S>是一个类型,表示一个有限对的集合,其中,T和S分别称为码类型和范围类型。每个对由两部分构成:T的值和S的值,其中T的值在集合中必须唯一。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步