正在加载图片...
1180 GENERICITY VERSUS INHERITANCE SB.4 A few loop details have been left out,as well as the body of infix "*"but they are easy to fill in.Features put and item as applied to implementation will come from the library class ARRAY2 describing two-dimensional arrays. To define the equivalent of the Ada generic package derivation shown earlier package BOOLEAN MATRICES is new MATRICES (BOOLEAN,false,true,"or","and"); we must first declare the"ring element"corresponding to booleans: class BOOLEAN RING ELEMENT inherit RING ELEMENT redefine zero,unity end creation put feature--Initialization put (v:BOOLEAN)is --Initialize from v. do item :=y end feature--Access item:BOOLEAN feature--Basic operations infix "+(other:like Current):like Current is -Boolean addition:or do !Result.put (item or other.item)end infix "*(other:like Current):like Current is --Boolean multiplication:and do !Result.put (item and other.item)end zero:like Current is --Zero element for boolean addition once !Result.put (False)end unity:like Current is --Zero element for boolean multiplication once !Result.put (True)end end--class BOOLEAN RING ELEMENT Note how zero and uniry are effected as once functions. Then to obtain an equivalent to the Ada package derivation,just define an heir BOOLEAN MATRIX of MATRIX,where you only need to redefine anchor,the artificial anchor;all the other affected types will follow automatically:1180 GENERICITY VERSUS INHERITANCE §B.4 A few loop details have been left out, as well as the body of infix "∗", but they are easy to fill in. Features put and item as applied to implementation will come from the library class ARRAY2 describing two-dimensional arrays. To define the equivalent of the Ada generic package derivation shown earlier package BOOLEAN_MATRICES is new MATRICES (BOOLEAN, false, true, "or", "and"); we must first declare the “ring element” corresponding to booleans: class BOOLEAN_RING_ELEMENT inherit RING_ELEMENT redefine zero, unity end creation put feature -- Initialization put (v: BOOLEAN) is -- Initialize from v. do item := v end feature -- Access item: BOOLEAN feature -- Basic operations infix "+" (other: like Current): like Current is -- Boolean addition: or do !! Result ● put (item or other ● item) end infix "∗" (other: like Current): like Current is -- Boolean multiplication: and do !! Result ● put (item and other ● item) end zero: like Current is -- Zero element for boolean addition once !! Result ● put (False) end unity: like Current is -- Zero element for boolean multiplication once !! Result ● put (True) end end -- class BOOLEAN_RING_ELEMENT Note how zero and unity are effected as once functions. Then to obtain an equivalent to the Ada package derivation, just define an heir BOOLEAN_MATRIX of MATRIX, where you only need to redefine anchor, the artificial anchor; all the other affected types will follow automatically:
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有