正在加载图片...
SB.4 EMULATING GENERICITY WITH INHERITANCE 1183 feature (NONE)--Implementation item anchor:OUEUABLE is do end implementation:ARRAY [like item anchor] successor (n:INTEGER):INTEGER is -Next value after n,cyclically in the interval /.capacity require n>=I;n<=capacity do Result:=(n capacity)+I end invariant 0<=count,count <capacity;first >=1;next>=I (not full)implies ((first <capacity)and (next<=capacity)) (capacity =0)implies full --Items,if any,appear in array positions first,...next-I(cyclically) end -class OUEUE For an alterative Bounded queue implementations elsewhere in this book rely on the technique of keeping technique see e-g.“A one position open.Here,we allocate capacity elements and keep track of count.There is buffer is a separate no particular reason,other than to illustrate alternative implementation techniques. queue",page 990. To get the equivalent of generic derivation (so as to obtain queues of a specific type) you must,as with the COMPARABLE example,define descendants of OUEUABLE: class INTEGER OUEUABLE inherit QUEUABLE creation put feature --Initialization put (n:INTEGER)is --Initialize from n. do item :=n end feature--Access item:INTEGER feature (NONE)--Implementation item anchor:INTEGER is do end end--class INTEGER OUEUABLE and similarly STRING OUEUABLE etc.;then declare the corresponding descendants of QUEUE,redefining item_anchor appropriately in each. Emulating unconstrained genericity It is possible to emulate unconstrained genericity through inheritance,by using wrapper classes and the corresponding wrapper objects.§B.4 EMULATING GENERICITY WITH INHERITANCE 1183 feature {NONE} -- Implementation item_anchor: QUEUABLE is do end implementation: ARRAY [like item_anchor] successor (n: INTEGER): INTEGER is -- Next value after n, cyclically in the interval 1 .. capacity require n >= 1; n <= capacity do Result := (n \\ capacity) + 1 end invariant 0 <= count; count <= capacity; first >= 1; next >= 1 (not full) implies ((first <= capacity) and (next <= capacity)) (capacity = 0) implies full -- Items, if any, appear in array positions first, … next – 1 (cyclically) end -- class QUEUE Bounded queue implementations elsewhere in this book rely on the technique of keeping one position open. Here, we allocate capacity elements and keep track of count. There is no particular reason, other than to illustrate alternative implementation techniques. To get the equivalent of generic derivation (so as to obtain queues of a specific type) you must, as with the COMPARABLE example, define descendants of QUEUABLE: class INTEGER_QUEUABLE inherit QUEUABLE creation put feature -- Initialization put (n: INTEGER) is -- Initialize from n. do item := n end feature -- Access item: INTEGER feature {NONE} -- Implementation item_anchor: INTEGER is do end end -- class INTEGER_QUEUABLE and similarly STRING_QUEUABLE etc.; then declare the corresponding descendants of QUEUE, redefining item_anchor appropriately in each. Emulating unconstrained genericity It is possible to emulate unconstrained genericity through inheritance, by using wrapper classes and the corresponding wrapper objects. For an alternative technique see e.g. “A buffer is a separate queue”, page 990
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有