正在加载图片...
172 THE STATIC STRUCTURE:CLASSES $7.5 Reaching the goal of a fully consistent and uniform type system requires the combination of several important O-O techniques,to be seen only later:expanded classes, to ensure proper representation of simple values;infix and prefix operators,to enable usual arithmetic syntax (such as a b or-a rather than the more cumbersome a.less than(b)or a.negated);constrained genericity,needed to define classes which may be adapted to various types with specific operations,for example a class MATR/Y that can represent matrices of integers as well as matrices of elements of other numeric types. 7.5 A SIMPLE CLASS Let us now see what classes look like by studying a simple but typical example,which shows some of the fundamental properties applicable to almost all classes. The features The example is the notion ofpoint,as it could appear in a two-dimensional graphics system A point and its coordinates To characterize type POINT as an abstract data type,we would need the four query functions x,y,p,0.(The names of the last two will be spelled out as rho and theta in software texts.)Function x gives the abscissa of a point (horizontal coordinate),y its ordinate(vertical coordinate),p its distance to the origin,0 the angle to the horizontal axis. The values ofx and y for a point are called its cartesian coordinates,those of p and e its polar coordinates.Another useful query function is distance,which will yield the distance between two points. Then the ADT specification would list commands such as translate (to move a point The name translate by a given horizontal and vertical displacement),rotate (to rotate the point by a certain refers to the "trans- angle,around the origin)and scale (to bring the point closer to or further from the origin lation"operation of geometry. by a certain factor). It is not difficult to write the full ADT specification including these functions and some of the associated axioms.For example,two of the function signatures will be x:POINT-→REAL translate:POINT X REAL×REAL→POINT and one of the axioms will be(for any point p and any reals a,b): x (translate (pl,a,b))=x(pl)+a expressing that translating a point by <a.b>increases its abscissa by a.172 THE STATIC STRUCTURE: CLASSES §7.5 Reaching the goal of a fully consistent and uniform type system requires the combination of several important O-O techniques, to be seen only later: expanded classes, to ensure proper representation of simple values; infix and prefix operators, to enable usual arithmetic syntax (such as a < b or –a rather than the more cumbersome a ● less_ than (b) or a ● negated); constrained genericity, needed to define classes which may be adapted to various types with specific operations, for example a class MATRIX that can represent matrices of integers as well as matrices of elements of other numeric types. 7.5 A SIMPLE CLASS Let us now see what classes look like by studying a simple but typical example, which shows some of the fundamental properties applicable to almost all classes. The features The example is the notion of point, as it could appear in a two-dimensional graphics system. To characterize type POINT as an abstract data type, we would need the four query functions x, y, ρ, θ. (The names of the last two will be spelled out as rho and theta in software texts.) Function x gives the abscissa of a point (horizontal coordinate), y its ordinate (vertical coordinate), ρ its distance to the origin, θ the angle to the horizontal axis. The values of x and y for a point are called its cartesian coordinates, those of ρ and θ its polar coordinates. Another useful query function is distance, which will yield the distance between two points. Then the ADT specification would list commands such as translate (to move a point by a given horizontal and vertical displacement), rotate (to rotate the point by a certain angle, around the origin) and scale (to bring the point closer to or further from the origin by a certain factor). It is not difficult to write the full ADT specification including these functions and some of the associated axioms. For example, two of the function signatures will be x: POINT → REAL translate: POINT × REAL × REAL → POINT and one of the axioms will be (for any point p and any reals a, b): x (translate (p1, a, b)) = x (p1) + a expressing that translating a point by <a, b> increases its abscissa by a. A point and its coordinates θ ρ p1 x y The name translate refers to the “trans￾lation” operation of geometry
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有