正在加载图片...
SB.4 EMULATING GENERICITY WITH INHERITANCE 1177 deferred class RING ELEMENT feature infix "+"(other:like Current):like Current is deferred ensure equal (other,zero)implies equal (Result,Current) end; infix "*(other:like Current):like Current is deferred end zero:like Current is deferred end unity:like Current is deferred end end--class RING ELEMENT Unlike Ada,the O-O notation allows us here to express abstract semantic properties, although only one of them has been included as an example (the property thatx+0=x for any x,appearing as a postcondition of infix "+") “ANCHORED The use of anchored types(like Current)makes it possible to avoid some improper DECLARATION”, combinations,as explained for the COMPARABLE example next.At this stage replacing 16.7,page599. all such types by R/NG ELEMENT would not affect the discussion. Constrained genericity:routines We can write a routine such as minimum by specifying its arguments to be of type COMPARABLE.Based on the Ada pattern,the function would be declared as minimum (one:COMPARABLE;other:like one):like one is --Minimum of one and other do...end In O-O development,however,every routine appears in a class and is relative to the current instance of that class;we may include minimum in class COMPARABLE, argument one becoming the implicit current instance.The class becomes: COMPARABLE deferred class COMPARABLE feature becomes a"behavior infix "<="(other:like Current):BOOLEAN is class",with an effec- tive feature relying --Is current object less than or equal to other? on a deferred one. deferred See "Don't call us, we'll call you”,page end 505. minimum (other:like Current):like Current is --Minimum of current object and other do if Current <other then Result:=Current else Result other end end end--class COMPARABLE To compute the minimum of two elements,you must declare them of some effective descendant type of COMPARABLE,for which infix "<="has been effected,such as§B.4 EMULATING GENERICITY WITH INHERITANCE 1177 deferred class RING_ELEMENT feature infix "+" (other: like Current): like Current is deferred ensure equal (other, zero) implies equal (Result, Current) end; infix "✳" (other: like Current): like Current is deferred end zero: like Current is deferred end unity: like Current is deferred end end -- class RING_ELEMENT Unlike Ada, the O-O notation allows us here to express abstract semantic properties, although only one of them has been included as an example (the property that x + 0 = x for any x, appearing as a postcondition of infix "+"). The use of anchored types (like Current) makes it possible to avoid some improper combinations, as explained for the COMPARABLE example next. At this stage replacing all such types by RING_ELEMENT would not affect the discussion. Constrained genericity: routines We can write a routine such as minimum by specifying its arguments to be of type COMPARABLE. Based on the Ada pattern, the function would be declared as minimum (one: COMPARABLE; other: like one): like one is -- Minimum of one and other do … end In O-O development, however, every routine appears in a class and is relative to the current instance of that class; we may include minimum in class COMPARABLE, argument one becoming the implicit current instance. The class becomes: deferred class COMPARABLE feature infix "<=" (other: like Current): BOOLEAN is -- Is current object less than or equal to other? deferred end minimum (other: like Current): like Current is -- Minimum of current object and other do if Current <= other then Result := Current else Result := other end end end -- class COMPARABLE To compute the minimum of two elements, you must declare them of some effective descendant type of COMPARABLE, for which infix “<=” has been effected, such as “ANCHORED DECLARATION”, 16.7, page 599. COMPARABLE becomes a “behavior class”, with an effec￾tive feature relying on a deferred one. See “Don’t call us, we’ll call you”, page 505
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有