类的声明由简单地三部分组成,就是关键字Class、类的名称、类的特性(property)表:
Class <name>{ <list of property>
}
同时,在ODL中,类的属性、方法和类之间的联系称为类的特性
属性声明也是由三部分组成,即关键字attribute、类型和属性名称: attribute <type><name>
[例子1]定义一个学生类和课程类:
Class Student{ attribute string Sno;
attribute string Sname;
attributestring Ssex;
attribute integer Sage;
attribute string Sdept;
}
Class Course{ attribute string Cno;
attribute string Cname;
attribute string Cpno;
attribute integer Ccredit;
}
string和integer是原子类型。面向对象数据模型还允许使用构造类型,例如,给学生类增加一个PhoneList属性,用于存放学生的电话号码,可以用string存储电话号码。 attribute set<string> PhoneList
方法有方法名字、输入参数、输出参数、返回值和抛出的异常。类的方法可以由类的所有对象调用。方法至少要有一个输入参数,它是类的对象,这个参数是隐含的,方法的调用者就是这个参数。 同一个函数名可以作为不同的类的方法,由于有隐含的输入参数,所以,虽然方法的名字相同,但是方法却是不同的方法,这样就达到了重载(overload)的目的。
Class Student{ attribute string Sno;
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{ attribute string Sno; attribute string Sname; attribute string Ssex; attribute integer Sage; attribute string Sdept; Relationship set <Course> Courses inverse Course::Students; interger ShowName(out string);}
Class Course{ attribute string Cno;
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){ ....
}
ODL支持单继承和多继承。研究生类是学生的子类,它继承了学生类的所有特性,还有自己的导师属性。 Class Postgraduate extends Student{ attribute string Supervisor;
}
如果类C是类C1、C2、...、Ca的子类: Class <name>extends C1:C2....Cn{ <list of property>
}
外延是类的对象的集合.面向对象数据库使用外延存储对象,实现对象的持久化.一般情况下,类的名字是单数名词,而外延是双数名词. ODL的语法为
Class <name>(extent <extentName>){ <list of property>
}
Student类的完整说明,包括码和外延Class Student(extent Students key Sno){ attribute string Sno;
attribute string Sname;
attribute string Ssex;
atribute integer Sage;
attribute string Sdept;
relationship set<Course>Courses inverse Course::Students;
interger ShowName(out string);
}
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的值在集合中必须唯一。
转载于:https://www.cnblogs.com/Raodi/p/11553409.html
