正在加载图片...
1170 GENERICITY VERSUS INHERITANCE SB.I generic type G is private; package OUEUES is type QUEUE(capacity:POSITIVE)is private; function empty (s:in OUEUE)return BOOLEAN: procedure add (t:in G;s:in out OUEUE): procedure remove (s:in out OUEUE) function oldest (s:in QUEUE)return G; private type OUEUE (capacity:POSITIVE)is --The package uses an array representation for queues record implementation:array (0..capacity)of G; count:NATURAL; end record; end OUEUES: Again this does not define a package but a package pattern;to get a directly usable package you will use generic derivation,as in package INT_QUEUES is new QUEUES (INTEGER); package STR_QUEUES is new QUEUES(STRING). Note again the tradeoff that generic declarations achieve between typed and untyped approaches.OUEUES is a pattern for modules implementing queues of elements of all possible types G,while retaining the possibility to enforce type checks for a specific G,so as to rule out such unholy combinations as the insertion of an integer into a queue of strings. The form of genericity illustrated by both of the examples seen so far,swapping and queues,may be called unconstrained since there is no specific requirement on the types that may be used as actual generic parameters:you may swap the values of variables of any type and create queues of values of any type,as long as all the values in a given queue are of the same type. Other generic definitions,however,only make sense ifthe actual generic parameters satisfy some conditions.This form may be called constrained genericity. Constrained genericity As in the unconstrained case,the examples of constrained genericity will include both a routine and a package. Assume first you need a generic function to compute the minimum of two values. You can try the pattern of swap: generic From here on most routine declarations type G is private; omit the in mode function minimum (x,y:G)return G is begin specification for ifx <=y then return x;else return y;end if; arguments,ph加chis end21171u71, optional.1170 GENERICITY VERSUS INHERITANCE §B.1 generic type G is private; package QUEUES is type QUEUE (capacity: POSITIVE) is private; function empty (s: in QUEUE) return BOOLEAN; procedure add (t: in G; s: in out QUEUE); procedure remove (s: in out QUEUE); function oldest (s: in QUEUE) return G; private type QUEUE (capacity: POSITIVE) is -- The package uses an array representation for queues record implementation: array (0 .. capacity) of G; count: NATURAL; end record; end QUEUES; Again this does not define a package but a package pattern; to get a directly usable package you will use generic derivation, as in package INT_QUEUES is new QUEUES (INTEGER); package STR_QUEUES is new QUEUES (STRING); Note again the tradeoff that generic declarations achieve between typed and untyped approaches. QUEUES is a pattern for modules implementing queues of elements of all possible types G, while retaining the possibility to enforce type checks for a specific G, so as to rule out such unholy combinations as the insertion of an integer into a queue of strings. The form of genericity illustrated by both of the examples seen so far, swapping and queues, may be called unconstrained since there is no specific requirement on the types that may be used as actual generic parameters: you may swap the values of variables of any type and create queues of values of any type, as long as all the values in a given queue are of the same type. Other generic definitions, however, only make sense if the actual generic parameters satisfy some conditions. This form may be called constrained genericity. Constrained genericity As in the unconstrained case, the examples of constrained genericity will include both a routine and a package. Assume first you need a generic function to compute the minimum of two values. You can try the pattern of swap: generic type G is private; function minimum (x, y: G) return G is begin if x <= y then return x; else return y; end if; end minimum; From here on most routine declarations omit the in mode specification for arguments, which is optional
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有