面向對象數據模型的構建和分析


一、類的聲明

      類的聲明由簡單地三部分組成,就是關鍵字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){
                  ....

          }

六、子類(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 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的值在集合中必須唯一。

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM