正在加载图片...
1176 GENERICITY VERSUS INHERITANCE SB.4 B.4 EMULATING GENERICITY WITH INHERITANCE Let us see if we will have more luck with the reverse problem:can we achieve the effect of Ada-style genericity in an object-oriented language with inheritance? The O-O notation introduced in earlier chapters does provide a generic parameter mechanism.But since we are comparing pure genericity versus pure inheritance,the rule of the game for some time,frustrating as it may be,is to pretend we have all but forgotten about that genericity mechanism.As a result the solutions presented in this section will be substantially more complex than those obtainable with the full notation,described in the rest of this book and in later sections.As you read this section,remember that the software extracts are not final forms,but for purposes of discussion only. Surprisingly perhaps,the simulation turns out to be easier,or at least less artificial, for the more sophisticated form of genericity:constrained.So we begin with this case. Emulating constrained genericity:overview The idea is to associate a class with a constrained formal generic type parameter.This is a natural thing to do since a constrained generic type may be viewed,together with its constraining operations,as an abstract data type.Consider for example the Ada generic clauses in our two constrained examples,minimum and matrices: generic This extract is in Ada type G is private, syntax. with function"<="(a,b:G)return BOOLEAN is < generic type G is private: 三ero:G,1mi:G, with function"+"(a,b:G)return G is <> with function ""(a,b:G)return G is <> We may view these clauses as the definitions of two abstract data types, COMPARABLE and RING ELEMENT;the first is characterized by a comparison operation"<=",and the second by features zero,unity,"+"and "*" In an object-oriented language,such types may be directly represented as classes. We cannot define these classes entirely,for there is no universal implementation of"<=", "+"etc.;rather,they are to be used as ancestors of other classes,corresponding to actual generic parameters.Deferred classes provide exactly what we need: deferred class COMPARABLE feature This extract and all infix "<="(other:COMPARABLE):BOOLEAN is deferred end remaining ones are in the 0-0 notation end -class COMPARABLE of this book.1176 GENERICITY VERSUS INHERITANCE §B.4 B.4 EMULATING GENERICITY WITH INHERITANCE Let us see if we will have more luck with the reverse problem: can we achieve the effect of Ada-style genericity in an object-oriented language with inheritance? The O-O notation introduced in earlier chapters does provide a generic parameter mechanism. But since we are comparing pure genericity versus pure inheritance, the rule of the game for some time, frustrating as it may be, is to pretend we have all but forgotten about that genericity mechanism. As a result the solutions presented in this section will be substantially more complex than those obtainable with the full notation, described in the rest of this book and in later sections. As you read this section, remember that the software extracts are not final forms, but for purposes of discussion only. Surprisingly perhaps, the simulation turns out to be easier, or at least less artificial, for the more sophisticated form of genericity: constrained. So we begin with this case. Emulating constrained genericity: overview The idea is to associate a class with a constrained formal generic type parameter. This is a natural thing to do since a constrained generic type may be viewed, together with its constraining operations, as an abstract data type. Consider for example the Ada generic clauses in our two constrained examples, minimum and matrices: generic type G is private; with function "<=" (a, b: G) return BOOLEAN is <> generic type G is private; zero: G; unity: G; with function "+" (a, b: G) return G is <>; with function "✳" (a, b: G) return G is < >; We may view these clauses as the definitions of two abstract data types, COMPARABLE and RING_ELEMENT; the first is characterized by a comparison operation "<=", and the second by features zero, unity, "+" and "✳". In an object-oriented language, such types may be directly represented as classes. We cannot define these classes entirely, for there is no universal implementation of "<=", "+" etc.; rather, they are to be used as ancestors of other classes, corresponding to actual generic parameters. Deferred classes provide exactly what we need: deferred class COMPARABLE feature infix “<=” (other: COMPARABLE): BOOLEAN is deferred end end -- class COMPARABLE This extract is in Ada syntax. This extract and all remaining ones are in the O-O notation of this book
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有