正在加载图片...
226 THE RUN-TIME STRUCTURE:OBJECTS $8.1 Declaring references Let us see how to extend the initial book class,BOOK/,which only had attributes of basic types,to the new variant BOOK3 which has an attribute representing references to potential authors.Here is the class text,again just showing the attributes;the only difference is an extra attribute declaration at the end: class BOOK3 feature title:STRING date,page_count:INTEGER author:WRITER -This is the new attribute. end The type used to declare author is simply the name of the corresponding class: WRITER.This will be a general rule:whenever a class is declared in the standard form class C feature...end then any entity declared of type C through a declaration of the form x:C denotes values that are references to potential objects of type C.The reason for this See page272. convention is that using references provides more flexibility,and so are appropriate in the vast majority of cases.You will find further examination of this rule (and of the other possible conventions)in the discussion section of this chapter. Self-reference Nothing in the preceding discussion precludes an object Ol from containing a reference field which (at some point of a system's execution)is attached to Ol itself.This kind of self-reference can also be indirect.In the situation pictured below,the object with "Almaviva"in its name field is its own landlord (direct reference cycle);the object "Figaro"loves "Susanna"which loves"Figaro"(indirect reference cycle). Direct and name "Almaviva" indirect self- reference landlord loved one (PERSONI name "Figaro" Susanna" name landlord landlord loved one loved one (PERSONI) (PERSONI226 THE RUN-TIME STRUCTURE: OBJECTS §8.1 Declaring references Let us see how to extend the initial book class, BOOK1, which only had attributes of basic types, to the new variant BOOK3 which has an attribute representing references to potential authors. Here is the class text, again just showing the attributes; the only difference is an extra attribute declaration at the end: class BOOK3 feature title: STRING date, page_count: INTEGER author: WRITER -- This is the new attribute. end The type used to declare author is simply the name of the corresponding class: WRITER. This will be a general rule: whenever a class is declared in the standard form class C feature … end then any entity declared of type C through a declaration of the form x: C denotes values that are references to potential objects of type C. The reason for this convention is that using references provides more flexibility, and so are appropriate in the vast majority of cases. You will find further examination of this rule (and of the other possible conventions) in the discussion section of this chapter. Self-reference Nothing in the preceding discussion precludes an object O1 from containing a reference field which (at some point of a system’s execution) is attached to O1 itself. This kind of self-reference can also be indirect. In the situation pictured below, the object with "Almaviva" in its name field is its own landlord (direct reference cycle); the object "Figaro" loves "Susanna" which loves "Figaro" (indirect reference cycle). See page 272. Direct and indirect self￾reference (PERSON1) name "Almaviva" landlord loved_one (PERSON1) name "Figaro" landlord loved_one (PERSON1) "Susanna" name landlord loved_one
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有